Closed
Description
TypeScript Version: 3.5.0-dev.20190518
Search Terms: narrow generic
Code
const isNull = (_: null): true => true;
const fails = <T>(arg: T | null): T => {
if (arg != null) {
// arg is T
return arg;
} else {
// 3.4.5: arg is null
// 3.5.0-dev.20190518: Argument of type 'T | null' is not assignable to parameter of type 'null'.
// Type 'T' is not assignable to type 'null'.
isNull(arg);
throw new Error();
}
};
Expected behavior:
With strict null checking, narrowing arg: T | null
to T
works. The else case of arg != null
should narrow to arg: null
.
Actual behavior:
In the else case, arg is not narrowed at all. This works as expected in 3.4.5, but appears to have regressed in 3.5.0.
Related Issues:
Metadata
Metadata
Assignees
Labels
No labels