Description
π Search Terms
generic type constraint not applied inside type
π Version & Regression Information
- This changed in commit or PR Divide-and-conquer strategy for intersections of unionsΒ #57871
β― Playground Link
π» Code
type AnyKey = number | string | symbol;
type ReturnTypeKeyof<Obj extends object> = Obj extends object
? [keyof Obj] extends [never]
? never
: { [Key in keyof Obj as string]-?: () => Key }[string]
: never;
type KeyIfSignatureOfObject<
Obj extends object,
Key extends AnyKey,
ReturnTypeKeys = ReturnTypeKeyof<Obj>,
> = ReturnTypeKeys extends () => Key ? ((() => Key) extends ReturnTypeKeys ? Key : never) : never;
export type Reduced<Obj extends object, Key extends AnyKey, Value, ObjKeys extends keyof Obj = keyof Obj> =
Key extends KeyIfSignatureOfObject<Obj, Key>
? Key extends ObjKeys // Always true.
? { [K in Key]: Value }
: never
: never;
π Actual behavior
Gives error Type 'ObjKeys & KeyIfSignatureOfObject<Obj, Key, ReturnTypeKeyof<Obj>> & Key' is not assignable to type 'string | number | symbol'.
π Expected behavior
No error; Key
was constrained to AnyKey
and should be usable as a key.
Additional information about the issue
I reduced this from a much larger example, so the code here doesn't make much sense (the names of the helper types might also not make sense anymore). It might be possible to reduce it further but I wasn't able to find anything obvious.
As the playground shows, simply wrapping the body of the type with a Key extends AnyKey
check makes the error go away, which makes me think that this must be a bug (I guess it distributes the union, but the entire union should be valid as a key type). Checking Key extends ObjKeys
before KeyIfSignatureOfObject
also makes the error go away.