Closed
Description
Bug Report
π Search Terms
Generic types nonnullable
π Version & Regression Information
This bug appears to exist ever since NonNullable was introduced (tested from 3.3.3 to 4.5.4)
β― Playground Link
Playground link with relevant code
π» Code
function throwIfNullable<T>(
value: T,
): NonNullable<T> {
if (value !== undefined && value !== null) {
return value;
}
throw Error('Nullable')
}
π Actual behavior
The line return value
raises a compilation error Type 'T' is not assignable to type 'NonNullable<T>'.
π Expected behavior
No compilation error as the if clause properly guards against nullish values
Activity
[-]NonNullable not properly casting generic types when excluding nullish values[/-][+]NonNullable not properly casting generic type when excluding nullish values[/+]jcalz commentedon Feb 26, 2022
Feels like #33912
fatcerberus commentedon Feb 26, 2022
An alternative construction that works would be:
RyanCavanaugh commentedon Feb 28, 2022
See e.g. #22348
typescript-bot commentedon Mar 3, 2022
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.