-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed as not planned
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π 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-ignorecomment 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
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed