microsoft / microsoft/TypeScript
Boolean narrowed to true only when generic type is explicitly provided while it shouldn't
Open
Nobody has claimed this yet.
Needs More Info
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
specify type narrow union
🕗 Version & Regression Information
All versions I checked and nothing in FAQ that seems related
⏯ Playground Link
💻 Code
// Use the variance annotation to force this type to be invariant, even though it would otherwise be covariant
type Invariant<in out T> = () => T;
const foo = <T extends object>(obj: T): Invariant<{[P in keyof T]: Invariant<T[P]>}> => null!;
const bar = <T extends {[P in keyof T]: Invariant<any>}>(w: T): T => null!;
const inferred = bar({a: foo({b: true})}); // true is boolean as expected
// Explicitly specify the generic param
const explicit = bar<{
a: Invariant<{
b: Invariant<boolean>;
}>
}>({a: foo({b: true})}); // narrowed to true instead of boolean and errors
🙁 Actual behavior
Narrows to true
🙂 Expected behavior
Should stay boolean
Additional information about the issue
Doing as boolean works...
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 repro and confirm the difference between inferred and explicitly supplied generic types, focusing on the boolean literal narrowing through the invariant mapped types. Trace the type-checking behavior that produces true instead of boolean, then add a regression test showing that the explicit call preserves boolean without requiring as boolean.
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