Open Tyriar opened 6 years ago
TypeScript narrowing doesn't do arbitrary Boolean algebra; it just looks for conjuncts of specific forms in the path condition of each path to the operation of interest. You could turn this issue into a suggestion, but I'm guessing it will be declined on performance grounds. Or you could just rewrite the code as:
function test(base: Base): void {
if (!base.b) {
if (!base.a) {
const result: null = null;
} else {
const result: {} = base.a;
}
} else {
const result: {} = base.b;
}
}
@mattmccutchen makes sense, it's a suggestion/feature request then. It seems like something that could make type narrowing even more powerful than it is, I'm not sure what the performance challenges are though.
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
@DanielRosenwasser I think this is labelled incorrectly and shouldn't have been closed, it never really got an answer on why or why not from a maintainer.
Sorry, I don't know why this was labeled as a question.
For the following code with
strictNullChecks
enabled:I'm seeing this error on the last result:
I expect it to work as the else implies:
TS play link