microsoft / microsoft/TypeScript
Boolean() cannot be used to perform a null check
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.4.0
Apologies for today's issue raising binge.
Code
// Compiles
const nullCheckOne = (value?: number) => {
if (!!value) {
return value.toFixed(0);
}
return '';
}
const nullCheckTwo = (value?: number) => {
if (Boolean(value)) {
// Object is possibly 'undefined'
return value.toFixed(0);
}
return '';
}
Expected behavior:
Both examples compile.
Actual behavior:
The latter example fails w/ Object is possibly 'undefined'.
Explanation
To my knowledge !!value and Boolean(value) are equivalent. I'm wondering what is the reason behind not supporting the second case. One reason I can think of would be an imported, non-typescript module, globally overriding it to something like: Boolean = (value) => !value.
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 by compiling the provided TypeScript 2.4 example and compare control-flow narrowing for !!value with Boolean(value). No source file or test path is named in the issue, so trace the type-checking and narrowing entry points before adding a regression test. Done means both forms narrow value sufficiently for toFixed(0) without weakening unrelated checks.
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