Closed
Description
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
Boolean strictNullChecks
Code
Pre-requirements: you need to have strictNullChecks
enabled;
export interface Price {
price?: {
value: number
};
}
const a: Price = {};
if (a.price) {
console.log(a.price.value); // works as expected
}
if (Boolean(a.price)) {
console.log(a.price.value); // throws an error for a.price, Object is possible undefined
}
Expected behavior:
I would be expecting this check also works.
Actual behavior:
Actually, it brings an error Object is possible undefined
.
I think it's caused by the fact the Boolean()
internally convert its argument to any
, so TS lost check for real value for type Price
. The same behavior can be achieved with if (a.price as any)
.
It would be nice to have it fixed, but I'm not sure is it really possible.
Metadata
Metadata
Assignees
Labels
No labels