-
Notifications
You must be signed in to change notification settings - Fork 1.7k
MiniJsParser fails to compile []= operator #25053
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
Comments
I needed to have an anonymous JS class that worked like a map (dynamic keys), and this was my solution: @JS()
@anonymous
class JsObject<T> {
external factory JsObject();
external operator []=(String key, T value);
external operator [](String key);
} It works in Dartium, but the above stack trace is thrown when Dart2JS tries to compile the |
You are currently not allowed to specify operators for typed JS interop classes. We need to display an error message when this case is detected. |
The requirement this was meant to fulfil was that a JS api takes a JS Object with keys that are not fixed. The only other way I found to make it work was to use Is there another way? |
You can use import 'package:js/js.dart';
@JS()
@anonymous
class Description {
external factory Description({bool configurable, bool enumerable, value});
}
@JS('Object.defineProperty')
external void defineProperty(o, String prop, Description description);
void setValue(o, String key, value) =>
defineProperty(o, 'key', new Description(value: value));
@JS()
@anonymous
class A {}
void main() {
final a = new A();
setValue(a, 'key1', 1);
setValue(a, 'key2', true);
} |
Thanks @a14n! |
@a14n It doesn't work for me. For example the following code: import 'dart:html';
import 'package:js/js.dart';
void main() {
A a = new A();
setValue(a, 'key1', 1);
setValue(a, 'key2', true);
a['key3'] = 1;
a['key4'] = true;
print(stringify(a));
}
@JS()
@anonymous
class Description {
external factory Description({bool configurable, bool enumerable, value});
}
@JS('Object.defineProperty')
external void defineProperty(o, String prop, Description description);
setValue(o, String key, value) =>
defineProperty(o, key, new Description(value: value));
@JS()
@anonymous
class A {
external factory A();
external void operator []=(String key, value);
}
@JS('JSON.stringify')
external String stringify(Object json); prints:
@jacob314 Is there any other way to do something like that, I'm now unable to js interop this case:
|
setValue(o, String key, value, {bool enumerable: true}) =>
defineProperty(o, key, new Description(value: value, enumerable: enumerable)); |
@a14n thanks, this works for me |
So instead of something like this, plain and readable (JS example):
I need to use some ugly workarounds with |
I'm on Dart VM version: 1.13.0 on "macos_x64"
The text was updated successfully, but these errors were encountered: