isA<T extends JSAny?> method
- @Since.new('3.4')
Whether this Object? is an instance of the JavaScript type
that is declared by T.
The result of this function tells you if you can safely cast to T.
Because of runtime type differences across platforms, do not expect that
that this function returns the same value for the same type on every
platform e.g. 0.isA.
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 and intrinsic functions in order to do this check. Use this instead
of is checks for platform-independent type 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 a few values for T that are exceptions to this rule:
JSTypedArray: AsTypedArraydoes not exist as a class in JavaScript, this does some prototype checking to makeisA<JSTypedArray>do the right thing.JSBoxedDartObject:isA<JSBoxedDartObject>will check if the value is a result of a previous ObjectToJSBoxedDartObject.toJSBox call.JSAny:isA<JSAny>will call an intrinsic function to check that the value is a JS value.JSObject:isA<JSObject>will call an intrinsic function to check that the value is a JS object (instanceof Objectis insufficient for some objects).JSExportedDartFunction:isA<JSExportedDartFunction>will check if the value is a result of a previous FunctionToJSExportedDartFunction.toJS or FunctionToJSExportedDartFunction.toJSCaptureThis call.- User interop types whose representation types are JS primitive types:
This will result in an error to avoid confusion on whether the user
interop type is used in the type-check. Use the primitive JS type as the
value for
Tinstead. - User interop types that have an object literal constructor: This will result in an error as you likely want to use JSObject instead.
Implementation
@Since('3.4')
external bool isA<T extends JSAny?>();