Description
TypeScript Version: 4.2.0-dev.20201109
Search Terms:
type narrowing truthy
type narrowing assertion
conditional throw
Code
function test(foo?: number) {
if (!foo) throw new Error();
return foo.toFixed();
}
function testWithLiteralTrue(foo?: number) {
if (!foo && true) throw new Error();
return foo.toFixed();
}
function testWithTruthyLiteral(foo?: number) {
if (!foo && "str") throw new Error();
return foo.toFixed(); // Object is possibly 'undefined'.
}
Expected behavior:
In all three functions typescript would know foo
is a number after the assertion.
Actual behavior:
In the last example, testWithTruthyLiteral
, typescript complains foo
may be undefined
. This is the same behavior for all known truthy values except for literal true
.
Related Issues:
#29323
It sounds like from #29323 (comment) that determining truthiness of expressions could lead to circular dependencies between compiler passes, but that literal true
has been special cased. This should be able to be extended to truthy literals without the circular dependency concern.
And for context, like others that have brought up similar issues, this stems from wanting a truthy token that can be used for a minification hook without affecting the types or code execution.