BugDomain: Mapped TypesHelp Wanted
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
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.