microsoft / microsoft/TypeScript
Type guard/Equality narrowing doesn't work on "any"
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
type guard equality narrowing strict equal equality
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I found earlier issues (https://github.com/microsoft/TypeScript/pull/9407 and the PR that added this feature https://github.com/microsoft/TypeScript/pull/9407)
⏯ Playground Link
💻 Code
function bar( arg: any ) {
if ( arg === null ) {
return arg;
}
if ( arg === false ) {
return arg;
}
}
🙁 Actual behavior
arg should be null/false inside the if conditions
🙂 Expected behavior
arg is any
The docs say this should work https://www.typescriptlang.org/docs/handbook/2/narrowing.html
When using unknown instead of any it works perfectly fine. I think adjusting this, so it works for any too, isn't too far fetched.
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
Reproduce the narrowing behavior in the linked TypeScript Playground and compare it with the documented behavior for unknown. Trace the type-checker path for equality narrowing of any, then add regression coverage showing null and false are narrowed as expected and run the relevant compiler tests.
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