Microsoft/TypeScript
GitHub ã§èŠãInconsistent boolean literal types in objects
Open
#59,754 opened on 2024幎8æ26æ¥
BugDomain: check: Type InferenceHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
boolean invariant type parameter literal widen union
ð Version & Regression Information
- This is the behavior in every version I tried
⯠Playground Link
ð» Code
// Box<T> type is invariant in T
type Box<T> = {
get: () => T;
set: (value: T) => void;
};
declare function box<T>(value: T): Box<T>;
const bn1 = box({ prop: 0 }); // Box<{ prop: number }>
const bn2: Box<{ prop: number }> = box({ prop: 0 }); // Ok
const bb1 = box({ prop: false }); // Box<{ prop: boolean }>
const bb2: Box<{ prop: boolean }> = box({ prop: false }); // Error, box<{ prop: false }> not assignable to Box<{ prop: boolean }>
ð Actual behavior
Error on bb2 is surprising and inconsistent with lack of error on bn2.
ð Expected behavior
Expected the box(false) expression to have type Box<boolean> for both bb1 and bb2.
Additional information about the issue
This issue is a small variation of https://github.com/microsoft/TypeScript/issues/48363 that was fixed by @ahejlsberg in https://github.com/microsoft/TypeScript/pull/48380