Skip to content
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

TS2536 only when type argument is a generic type parameter #59000

Closed
ryan-0324 opened this issue Jun 24, 2024 · 2 comments
Closed

TS2536 only when type argument is a generic type parameter #59000

ryan-0324 opened this issue Jun 24, 2024 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ryan-0324
Copy link

πŸ”Ž Search Terms

ts2536, ts2536 generic, generic constraint no effect

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic constraints.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// Type '"foo"' cannot be used to index type 'PartialOnUndefined<T>'. (2536)
type Generic<T extends { foo: string; }> = PartialOnUndefined<T>["foo"];

// No error
type NonGeneric = PartialOnUndefined<{ foo: string; }>["foo"];

type PartialOnUndefined<T>
    = { [Key in keyof T as undefined extends T[Key] ? never : Key]: T[Key]; }
    & { [Key in keyof T as undefined extends T[Key] ? Key : never]?: T[Key]; };

πŸ™ Actual behavior

An error is reported when indexing PartialOnUndefined<T> with type "foo" where generic type parameter T extends { foo: string; }:

Type '"foo"' cannot be used to index type 'PartialOnUndefined<T>'. (2536)

πŸ™‚ Expected behavior

There should be no error; TS should be able to determine that "foo" can always index PartialOnUndefined<T>, since T extends { foo: string; }.

Additional information about the issue

Possibly related to #36631

Workarounds:

  • Add a ts-ignore comment to silence the error; the type alias can still be used without issue.
  • Explicitly narrow PartialOnUndefined<T>:
type Generic<T extends { foo: string; }>
    = PartialOnUndefined<T> extends { foo: string; }
        ? PartialOnUndefined<T>["foo"]
        : never;
@ryan-0324 ryan-0324 changed the title TS2536 only when type parameter is generic TS2536 only when type argument is a generic type parameter Jun 24, 2024
@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 26, 2024
@RyanCavanaugh
Copy link
Member

I don't think we'll ever have the kind of logic that would be needed to determine that keyof PartialOnUndefined<T> was exactly the same as keyof T. Inspecting the two index signatures here to figure out that they are exactly complementary is too niche of a situation.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Design Limitation" 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 Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants