You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Toggle strictNullChecks off and see the type error appear// on line 7, while 2nd fn never errors and 3rd always errorsconstgetMaybeStringLen=(maybeString: string|void)=>{if(maybeString===null||maybeString===undefined){returnundefined;}returnmaybeString.length;};constgetMaybeStringLenBoolNegation=(maybeString: string|void)=>{if(!maybeString){returnundefined;}returnmaybeString.length;};constgetMaybeStringLenBoolCoercionNegation=(maybeString: string|void)=>{if(!Boolean(maybeString)){returnundefined;}returnmaybeString.length;};
Expected behavior:
strictNullChecks: false behaves less strictly than strictNullChecks: true. Admittedly a loose criterion, but it seems like the stricter check fails with strictNullChecks: false and passes with strictNullChecks: true, while the type coercive check passes in both cases.
My confusion stems from the fact that ! is a valid type guard for void for both compiler flags, while checking against null and undefined is valid for strictNullChecks: true only.
As a bonus, while ! is always valid, !Boolean(x) is never valid.
Actual behavior:
getMaybeStringLen...
...typechecks with strictNullChecks: true
...does NOT typecheck with strictNullChecks: false
getMaybeStringLenBoolNegation...
...typechecks with strictNullChecks: true
...typechecks with strictNullChecks: false
getMaybeStringLenBoolCoercionNegation...
...does NOT typecheck with strictNullChecks: true
...does NOT typecheck with strictNullChecks: false
TypeScript Version:
typescript@3.7.0-dev.20190831Search Terms: void guard strictNullChecks boolean negation refinement
Code
Expected behavior:
strictNullChecks: falsebehaves less strictly thanstrictNullChecks: true. Admittedly a loose criterion, but it seems like the stricter check fails withstrictNullChecks: falseand passes withstrictNullChecks: true, while the type coercive check passes in both cases.My confusion stems from the fact that
!is a valid type guard forvoidfor both compiler flags, while checking againstnullandundefinedis valid forstrictNullChecks: trueonly.As a bonus, while
!is always valid,!Boolean(x)is never valid.Actual behavior:
getMaybeStringLen...strictNullChecks: truestrictNullChecks: falsegetMaybeStringLenBoolNegation...strictNullChecks: truestrictNullChecks: falsegetMaybeStringLenBoolCoercionNegation...strictNullChecks: truestrictNullChecks: falsePlayground Link: http://www.typescriptlang.org/play/#code/PTAEBUHsHNoGwKagM4BcBOBLAxqgcgK5xwDCAFgtgNbKiQBm9oAhgHYAmKCSqFoqATwAOSBOnSR0LISOboAUCDqtQcTKyQB2ADSgA7mUyJQAJg6h6KjQDcxoMRPS02nAMzpOzOHuYDaDyWR5bEhWNFBoBFQAWV8AIwQAZQx1aAAZBBUAXlAACgBbeKSU1mgALhQS6FAAH1BrSEx2AEpQLIA+UABveVBQTCYCouSsUrasnNYiOFq6woEEkdTxnIIOBHp1BBbu3r7QdCiCdBU19g2t9gBuPYBfPcPUY5V5xaqAOkRS3hvbm+DQuFIjFhlUMqwAEKQSBwPAIaDMVCYUJtPKvYqjcqVTG1eqNHYdXZ9AZ5ACE6KWpVaPX2ByOJ1AZwuGmudwe9JeoMxn0y0B+8j+8gBYVQESisQWGNS4KhMJIkDE2GRrDhCKRKJyQ0llKxaBxdQaTVahJp-UGpNliDYWremOa1L2fUez0Z602LJufXuTo5oApHy+fLIvxuQA
Related Issues: #1806, #8322, #10564