Closed
Description
TypeScript Version: 3.5.0-dev.20190420
Search Terms: narrowing strictNullChecks
Code
type Box<T extends string> = { [key in T]: number | null }
function test1<T extends string>(key: T, { [key]: value }: Box<T>): number {
if (value != null) {
return value //ERROR: Type 'null' is not assignable to type 'number'.
}
return 0
}
function test2<T extends string>(key: T, box: Box<T>): number {
if (box[key] != null) {
return box[key] //ERROR: Type 'null' is not assignable to type 'number'.
}
return 0
}
Expected behavior:
In the block of if (value != null) {...}
, the value
is considered as non-null.
Actual behavior:
In the block of if (value != null) {...}
, the value
is considered as nullable.
(please turn strictNullChecks
option on.)
Related Issues:
Maybe #22137 is related.