microsoft / microsoft/TypeScript
TS doesn't narrow generic parameter T to null, despite an explicit comparison
Open
Nobody has claimed this yet.
Domain: check: Control Flow
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
T to null
🕗 Version & Regression Information
- This is a type narrowing issue
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
function isNull<T>(value: T) {
if (value === null) {
value // T, should be null
}
return value === null;
}
🙁 Actual behavior
value is T inside the if block of value === null (explicit comparison)
🙂 Expected behavior
value should be of type null inside the if block of value === null
Additional information about the issue
No response
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 reproduction and trace the compiler's type-narrowing behavior for the explicit value === null check on generic T. Confirm the result against the expected type shown in the example; done means value is treated as null inside the if block without regressing the return comparison.
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
- Clearly specified
- Newbie friendliness
- 45/100