microsoft / microsoft/TypeScript
Typescript does not recognize function Boolean() as a check for undefined
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
I followed the submission guidelines.
I prefer to check for a defined object using Boolean(object) rather than !!object, which I consider bad style. Typescript linter recognizes !! as a check for undefined but does not recognize Boolean() as a check for undefined.
I could not find a tsconfig or tslint configuration setting related to this behavior.
2.9.0-dev.20180511 (typescript@next)
"Boolean" "Boolean(" "function Boolean"
Code
This is not production code but a simple demonstration only to satisfy the expression tree.
function booleanTest() {
let value: Array<object> | undefined = undefined;
if (Date.now() > 1) {
value = [{}];
}
if (Boolean(value)) {
console.log(value.length);
}
}
Expected behavior:
No Typescript linter errors
Actual behavior:
Typescript linter in VS Code displays an error "[ts] Object is possibly 'undefined'." with a red squiggly on the final reference to variable "value".
Playground Link:
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 provided TypeScript example and investigate the type-checking path for control-flow narrowing through Boolean(value), comparing it with the recognized !!value form. Done means the example no longer reports that value may be undefined while preserving correct behavior for other Boolean calls.
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