typescript - How to apply a type guard for jQuery instance? -


i'm trying apply type guard variable might jquery instance. however, jquery interface jquery.d.ts, can't use instanceof. following code doesn't seem work, guess because ts doesn't "know" being instance of jquery means being "an instance" of jquery interface.

var stringorjquery: string | jquery; ... if (stringorjquery instanceof jquery) {   ...   // here stringorjquery still string | jquery   ... } 

i'm using ts 2.0 beta.

you can check string case:

if(typeof stringorjquery === 'string') {     // string case } else {     // jquery case case } 

alternately, can determine jquery object indirect checks the ones described in question's answers, such as:

if(stringorjquery.jquery !== undefined) { ... // jquery object 

the jquery field of jquery object contains jquery version , should defined.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -