isA<T extends JSAny?> method
- @Since('3.4')
Whether this JSAny?
is an instance of the JavaScript type
that is declared by T
.
Since the type-check this function emits is determined at compile-time,
T
needs to be an interop extension type that can also be determined at
compile-time. In particular, isA
can't be provided a generic type
variable as a type argument.
This method uses a combination of null
, typeof
, and instanceof
checks in order to do this check. Use this instead of is
checks.
If T
is a primitive JS type like JSString, this uses a typeof
check
that corresponds to that primitive type like typeofEquals('string')
.
If T
is a non-primitive JS type like JSArray or an interop extension
type on one, this uses an instanceof
check using the name or the
@JS
rename of the given type like
instanceOfString('Array')
. Note that if you rename the library using the
@JS
annotation, this uses the rename in the instanceof
check like instanceOfString('library1.JSClass')
.
To determine the JavaScript constructor to use as the second operand in
the instanceof
check, this function uses the JavaScript name associated
with the extension type, which is either the argument given to the
@JS
annotation or the Dart declaration name. So, if you had
an interop extension type JSClass
that wraps JSArray
without a rename,
this does an instanceOfString('JSClass')
check and not an
instanceOfString('Array')
check.
There are two exceptions to this rule. The first exception is
JSTypedArray
. As TypedArray
does not exist as a property in
JavaScript, this does some prototype checking to make isA<JSTypedArray>
do the right thing. The other exception is JSAny
. If you do a
isA<JSAny>
check, it will only do a null
check.
Using this method with a T
that has an object literal constructor will
result in an error as you likely want to use JSObject instead.
Using this method with a T
that wraps a primitive JS type will result in
an error telling you to use the primitive JS type instead.
Implementation
@Since('3.4')
external bool isA<T extends JSAny?>();