Closed
Description
TypeScript Version: 4.1.0-dev.20201026
Search Terms: assert comma operator
Code
foo1
and foo2
should be equivalent but the assertion in foo2
does not narrow they typing of param
.
declare function assert(value: any): asserts value;
function foo1(param: number | null | undefined): number | null {
const val = param !== undefined;
if (val) {
assert(param !== undefined);
return param
} else {
return null;
}
}
function foo2(param: number | null | undefined): number | null {
const val = param !== undefined;
return val ? (assert(param !== undefined), param) : null;
// ^^^^^ Still typed as number | null | undefined
}
Expected behavior:
No errors.
Actual behavior:
Type 'number | null | undefined' is not assignable to type 'number | null'.
Type 'undefined' is not assignable to type 'number | null'.(2322)
Related Issues: