microsoft / microsoft/TypeScript
Inconsistent boolean literal types in objects
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and reproduce the difference between the numeric and boolean Box examples. Trace the type inference and assignability behavior for the shown invariant Box declaration. Done means the boolean cases behave consistently with the numeric cases, with regression coverage for the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100