Boolean() cannot be used to perform a null check
**TypeScript Version:** 2.4.0 Apologies for today's issue raising binge. **Code** ```ts // 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`.
Posted by kujon on Jun 20, 2017, 7:03 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Boolean() cannot be used to perform a null check
**TypeScript Version:** 2.4.0 Apologies for today's issue raising binge. **Code** ```ts // 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`.
Comments (0)
No comments yet. Start the discussion.