The new style JS annotation does not support class constructors as properties of other classes #54731
Labels
area-web
Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop.
web-js-interop
Issues that impact all js interop
With the new
@JS()
annotation that is part of the new approach for JS interop (usingdart:js_interop
and friends),there is yet no way to "namespace" the names in the annotation.
In the old,
dart:html
-era@JS()
annotation, this was possible, because it handled dots in the name differently.Use Case
Developers wanting to integrate JS libraries that define the following pattern:
In this case, defining the following static interop definition does not work:
Getting access to the
MyLibrary
part of the namespace is possible, by using a static interop definition on the Window.But leveraging the static interop pattern to go further down in the namespace does not work,
as this is interpreted as
globalThis["MyLibrary.Foo"]
instead ofglobalThis["MyLibrary"].Foo
, as mentioned in #54531 (comment)Thanks to #54534 being resolved, we can work around this manually by invoking
callConstructor()
, but this is both error prone, and nullifies the benefits of the static typing mechanism of the JS interop system. Furthermore, adding the static interop type to the invocation, i.e.callConstructor<Foo>()
does not seem to work?I have also tried adding
at the top of the library unit, but to no avail.
Proposal
Support handling namespace-like constructs like in the use case above, by extending the existing interop tooling to support namespaces like in the example above. Maybe we could add a property to the JS annotation, to let developers indicate that it is a namespace subdivision, and not a dot in the name?
It is certainly possible that this is already somewhat possible today, but then I am unaware of the ways to do so.
In that case, documenting how to do it would be sufficient.
Dart 3.4.0-70.0.dev (dev) (Wed Jan 24 20:02:37 2024 -0800) on "macos_x64"
MacOS Sonoma 14.1.2 (Build 23B92)
The text was updated successfully, but these errors were encountered: