Skip to content

[js_interop] Make it an error to declare an external constructor on an interop extension type whose core interop type is not <: JSObject #61314

@sgrekhov

Description

@sgrekhov

The following code produces a run-time error on dart2js but works on dart2wasm.

import 'dart:js_interop';

@JS('Number')
extension type MyNumber._(JSNumber _) implements JSNumber {
  external MyNumber.fromDart(num value);
}

main() {
  // dart2wasm: prints 42
  // dart2js: TypeError: Instance of 'Number': type 'UnknownJavaScriptObject' is not a subtype of type 'double'
  print(MyNumber.fromDart(42));
}

Please note that according to the interop documentation MyNumber.fromDart(42) invokes constructor of JS Number using new. When this constructor is invoked with 'new' it returns "a wrapping Number object, which is not a primitive.". If the returned object is not a Number primitive then may be it's not a subtype of MyNumber representation type and this explains the error on dart2js? I don't know, it's just a suppose.

@srujzs @osa1 is this an issue or may be expected but undocumented behavior? What should be expected in this case?

Dart SDK version: 3.10.0-edge.f98e53cd40fe0abc7c7751b8146a0db9cea99758 (main) (Wed Aug 13 23:32:49 2025 -0700) on "linux_x64"

Metadata

Metadata

Assignees

Labels

area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.type-questionA question about expected behavior or functionalityweb-js-interopIssues that impact all js interop

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions