Closed
Description
TypeScript Version: 3.7.5
Search Terms: NonNullable void
Code
function isNonNull<T>(value: T): value is NonNullable<T> {
return value != null;
}
function test(): void | true {
if (Math.random() < 5) {return true;}
}
const t1 = test();
let t2: true;
if (isNonNull(t1)) {
t2 = t1;
}
Expected behavior:
should work fine, because != null
is also not void
Actual behavior:
getting Type 'void' is not assignable to type 'true'.
workaround
I'm using
type NonNullable2<T> = T extends null | undefined | void ? never : T;
which works fine
Metadata
Metadata
Assignees
Labels
No labels