Skip to content

Extend {} type checking failure #55546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aabeborn opened this issue Aug 28, 2023 · 2 comments
Closed

Extend {} type checking failure #55546

aabeborn opened this issue Aug 28, 2023 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@aabeborn
Copy link

πŸ”Ž Search Terms

type checking on extending generics

πŸ•— Version & Regression Information

  • This changed between versions 5.1.6 and 5.2.2

⏯ Playground Link

No response

πŸ’» Code

export interface ItemDef<T extends {}, K extends keyof T> {
  id: keyof T extends string ? keyof T : never;
  subItem?: keyof T;
  children?: T[K] extends Array<infer U> ? ItemDef<U extends {} ? U : never, keyof U> : ItemDef<T[K], keyof T[K]>;
  render: (props: RenderPropsItem<T>) => ReactNode;
  renderBody?: (props: RenderPropsItem<T>) => ReactNode;
}

interface RenderProps {
  id: string;
  [key: string]: unknown;
}

export type RenderPropsItem<T> = T & RenderProps;

export interface RenderItem {
  id: string;
  data: unknown;
  render: (...args: unknown[]) => ReactNode;
  renderBody?: (...args: unknown[]) => ReactNode;
  subItems?: RenderItem[];
  isExpanded?: boolean;
  isExpandable?: boolean;
  toggleExpanded: () => void;
}

πŸ™ Actual behavior

In the new 5.2 version the children property on type checking returns an error

Type error: Type 'T[K]' does not satisfy the constraint '{}'.
   Type 'T[keyof T]' is not assignable to type '{}'.
    Type 'T[string] | T[number] | T[symbol]' is not assignable to type '{}'.
      Type 'T[string]' is not assignable to type '{}'.

on line children?: T[K] extends Array<infer U> ? ItemDef<U extends {} ? U : never, keyof U> : ItemDef<T[K], keyof T[K]>;

while in the previous version the type check was fine

πŸ™‚ Expected behavior

Not sure if it was an error on previous version (5.1.6) or it's a regression.
If a regression to work fine

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Bisected it to this diff which clearly points to #54845 .

This code didn't typecheck with 5.0 and the fact that it did typecheck ok with 5.1 was a bug. This works as intended.

You need to find a way to add the appropriate constraint to T[K] there. I see that you are already doing this ItemDef<U extends {} ? U : never, keyof U> in the array-related branch of this conditional type. A similar thing would do the trick in the branch that doesn't typecheck today: ItemDef<T[K] extends {} ? T[K] : never, keyof T[K]>. That said, I'd probably try to improve the T's constraint on your place.

@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 28, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants