Skip to content

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

Open
@nex3

Description

@nex3

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)));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-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