Closed
Description
Bug Report
π Search Terms
- generic can't be narrowed
- narrow nonnullable generic
π Version & Regression Information
- This is the behavior in every version I tried from 4.6.2 to 4.0.5 and nightly v4.7.0-dev.20220408
β― Playground Link
Playground link with relevant code
π» Code
function test<T extends string | null>(input: T): NonNullable<T> {
if (!input) { // this should narrow `input` to `T extends string`
throw new Error();
}
return input; // type error
}
π Actual behavior
Type error
Type 'T' is not assignable to type 'NonNullable<T>'.
Type 'string | null' is not assignable to type 'NonNullable<T>'.
Type 'null' is not assignable to type 'NonNullable<T>'.
π Expected behavior
TypeScript should know I've narrowed the generic type to satisfied the NonNullable
type.
I came across #43183 which I was expecting to also address this scenario.