Strange boolean-discriminant narrowing with strictNullChecks off
See original GitHub issueTypeScript Version: nightly (2.1.0-dev.20160826)
Code
type Result = { success: true }
| { success: false, error: string }
function handleError(res: Result) {
if (res.success === true) {
return;
}
res.error; // OK
}
function handleError2(res: Result) {
if (res.success !== false) {
return;
}
res.error; // OK
}
function handleError3(res: Result) {
if (res.success) {
return;
}
res.error; // Property 'error' does not exist on type 'Result'
// but should be OK
}
Expected behavior: All three cases should behave the same.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:7 (4 by maintainers)
Top Results From Across the Web
strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is false , null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.
Read more >Refactoring Octopus: Adding strict null checks to the Octopus ...
Learn from some of the lessons we learned adding strict null checks to the Octopus front-end codebase.
Read more >Handbook Page - Microsoft Open Source
strictNullChecks off ; strictNullChecks on; Non-null Assertion Operator (Postfix ! ) ... Narrowing occurs when TypeScript can deduce a more specific type for...
Read more >typescript not null type guard - La Columnaria Blog
Previously, we discussed how all type guards are based around a boolean check. ... The reason why this doesn't work when strictNullChecks is...
Read more >Overview - TypeScript
Below, next() can only be called with boolean s, and depending on the value ... In TypeScript 3.5, when assigning to types with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, I just wanted to point out that this issue now has additional relevance in TypeScript 3.6 due to the addition of the
Generator
type and the newIteratorResult
type. I was writing some code with generators, expecting I could usewhile(!result.done)
to loop over the case where the generator was not done, but found in fact I had to usewhile(result.done === false)
. Might be a reason to revisit this?I have experienced similar behaviour, and the following code helped me to work around this limitation:
(Assuming we have
foo
with the discriminating boolean-property “discBool
” and the optional property “optionalProperty
” which is only present in one of the two sub-types offoo
’s super-type. In other words: IfdiscBool
is false,optionalProperty
is present, ifdiscBool
is true,optionalProperty
is not present infoo
)while this code didn’t work: