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
Post a Comment