Skip to content

JS interop can't create anonymous objects with keys that are Dart keywords #60666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nex3 opened this issue May 2, 2025 · 0 comments
Open
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop web-js-interop Issues that impact all js interop

Comments

@nex3
Copy link
Member

nex3 commented May 2, 2025

The set of reserved keywords in JS isn't exactly the same as in Dart, and numerous keywords that aren't allowed as instance member names in Dart are allowed in JS. This means that JS APIs not-infrequently use these names for options objects. The name default in particular is a keyword in Dart and a likely option in JS APIs.

Currently, Dart's JS interop can't easily construct options objects that use these keyword-style option names. I'd expect the following to work:

@anonymous
extension type KeywordBag._(JSObject _) implements JSObject {
  external KeywordBag({JSAny? defaultValue});

  @JS('default')
  external JSAny? get defaultValue;
}

But this example demonstrates that the object is actually constructed with a field named defaultValue, not default, despite the @JS annotation:

import 'dart:js_interop';

@JS('Object.getOwnPropertyNames')
external JSArray<JSString> getOwnPropertyNames(JSObject object);

@anonymous
extension type KeywordBag._(JSObject _) implements JSObject {
  external KeywordBag({JSAny? defaultValue});

  @JS('default')
  external JSAny? get defaultValue;
}

void main() {
  print(getOwnPropertyNames(KeywordBag(defaultValue: 123.toJS)));
}
@lrhn lrhn added web-js-interop Issues that impact all js interop area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop labels May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

2 participants