Skip to content

[Feature Request] dart:js_interop needs isJSAny and isJSObject for real-world use cases. #59932

@gmpassos

Description

@gmpassos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-enhancementA request for a change that isn't a bugweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions