How to use is not type in typescript?
example(just an example), I tried to use is not:
function notUndef(obj: any): obj is not undefined {
return obj !== void(0);
}
and
function notUndef(obj: any): (typeof obj !== undefined) {
return obj !== void(0);
}
but I received two error:
Cannot find name 'not'.ts(2304)
'{' or ';' expected.ts(1144)
both of them doesn’t work, what can I do?
>Solution :
You may be looking for combining generics with the type predicate:
function notUndef<T>(obj: T): obj is Exclude<T, undefined> {
return obj !== void(0);
}