Microsoft/TypeScript
View on GitHubGeneric type gets widened in an unexpected way
Open
#52,249 opened on Jan 15, 2023
BugDomain: Mapped TypesHelp Wanted
Description
Bug Report
๐ Search Terms
widen
๐ Version & Regression Information
4.9.4
โฏ Playground Link
Playground link with relevant code
๐ป Code
enum One {
A = 'a',
B = 'b',
C = 'c'
}
const isOneSomethingMap = {
[One.A]: true,
[One.B]: false,
[One.C]: true
} as const satisfies Record<One, boolean>;
type BooleanMapToUnion<T extends Record<string, boolean>> = {
[P in keyof T]: T[P] extends true ? P : never;
}[keyof T];
type SomethingOne = BooleanMapToUnion<typeof isOneSomethingMap>;
const a = <T>(value: T) => value;
const b = <T>(fn: (value: T) => T, v: T): T => fn(v);
const v: SomethingOne = One.A as SomethingOne;
const v2: One.A | One.C = One.A as One.A | One.C;
const r1 = b(a, v); // One and not SomethingOne - not expected
const r2 = a(v); // SomethingOne as expected
const r3 = b(a, v2); // Union as expected
const r4 = b(a, One.A as One.A | One.C); // Works
const r5 = b(a, One.A as SomethingOne); // Still doesn't work...
๐ Actual behavior
The type gets widened when using BooleanMapToUnion... I guess it is the culprit.
๐ Expected behavior
Both union and using BooleanMapToUnion should work equally.