Skip to content

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

@ryan-0324

Description

@ryan-0324

πŸ”Ž 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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions