Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use is not type in TypeScript?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading