-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Currently, dart:js_interop only provides isA from the JSAnyUtilityExtension to check for an extension type in the "JS world."
While migrating from dart:html to the web package, we encountered issues in real-world use cases.
The main problem arises when a "JS" instance (JSAny) is assigned to a variable of type Object. There's currently no official or secure way to cast an Object back to a JSAny. Technically, it is prohibitive to handle a JS type as Object because, once treated as an Object, you cannot safely cast it back to JSAny without risking a casting exception, invalid behavior, or even an invalid Wasm cast.
To address this, it would be helpful to have the following extension, with the correct implementation provided based on the platform (JS or Wasm):
extension ObjectToJSAnyNullableExtension on Object? {
bool get isJSAny;
bool get isJSObject;
}This would enable developers to correctly check if an Object is a JSAny or JSObject before attempting a cast.
isJSObject is also included because, in most cases, isA is used to check for a class type rather than a primitive type.
NOTE: After discussing this issue with other developers, we concluded that without a secure way to cast Object to JSAny, the new dart:js_interop is not stable for real-world use cases.