-
Notifications
You must be signed in to change notification settings - Fork 44
Add typings for JSRecord and some unsafe extensions for JSObject #487
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
base: main
Are you sure you want to change the base?
Conversation
This doesn't include JSObject extensions that require types that aren't defined yet, like JSSymbolicRecord.
|
We should do better on those JS failures in Wasm: dart-lang/sdk#62218 |
| /// The operation is equivalent to doing `this[key] = value` for each key and | ||
| /// associated value in [other]. It iterates over [other], which must therefore | ||
| /// not change during the iteration. | ||
| void addAllRecord(JSRecord<V> other) => addPairs(other.pairs); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit: maybe addAllFromRecord instead?
| @JS('Object.values') | ||
| external JSArray<JSAny?> _values(JSObject object); | ||
|
|
||
| /// Additional instance methods for the `dart:js_interop` [interop.JSObject] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove interop.
| /// See [`Object.entries()`]. | ||
| /// | ||
| /// [`Object.entries()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries | ||
| List<(String, JSAny?)> get entries => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on making this a generic method instead so that the second tuple member can be a generic (and therefore avoid the need for a cast list)?
| /// The [name] must be a [JSString] or a [JSSymbol]. | ||
| /// | ||
| /// [`Reflect.get()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get | ||
| R getPropertyWithThis<R extends JSAny?>(JSAny name, JSAny? thisArg) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this and setPropertyWithThis are sufficiently different in name that we may want a Reflect type instead, especially if we're planning to add more Reflect members. Thoughts?
js_interop/test/record_test.dart
Outdated
| ); | ||
| }); | ||
|
|
||
| test("addPairs()", () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: did you mean to name this as clear()?
| expect(record.containsKey("foo"), isTrue); | ||
| expect(record.containsKey("baz"), isFalse); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: test for containsValue
| () => | ||
| expect(object.getPropertyWithThis("foo".toJS, object), equals(1.toJS)), | ||
| ); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: test for getOwnProperty
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces JSRecord as a map-like wrapper for JS objects and adds several unsafe extensions for JSObject. The implementation is generally good, but I've found a few areas for improvement, mainly related to performance in JSRecord and some minor issues in documentation and tests. My review includes suggestions to optimize method implementations and fix typos.
|
Thank you for providing this! It's something I've really wanted officially in Dart JS Interop. I wanted to ask why we only make use of I'm mainly saying this because eventually, I'd want to replace the generated types we make using the web generator to these ones, so it'd be nice if these types could match rather than having to remake the same types. |
|
JS objects can't actually have numeric keys; numbers are coerced to strings. You can test this by running the following in the dev console: const obj = {};
obj[1] = 1;
obj["1"]Symbols are another matter. I plan to bring over another type, |
Agreed, but numeric indexes could also still work, and people/js package authors still rely on this functionality anyways. The main issue I'm having is that the packages I mentioned use custom types (usually unions, but there are instances of enums as well, which are usually
In that case, would it be fine if we have |
In principle, you can pass any value to an object key and it'll work—but it'll also coerce the value to a string. This is the way of a loosely-typed dynamic language. But JS interop generally tries to frame the APIs as strictly as possible, and a JS object can't meaningfully hold any keys that aren't strings or symbols. Since other key types will be coerced anyway, it's easy enough to do that coercion eagerly and pass them to the string API. It's also worth noting that the
The substantial majority of record-style object uses in JS are specifically string keys (I count about 75% |
|
@nikeokoronkwo Could the number/enum use-case be better handled if we had extra members that can set/update/etc. numeric keys? Since the numeric keys are stringified, should APIs like Re: the polymorphic option, we could have a base class for |
This doesn't include JSObject extensions that require types that
aren't defined yet, like JSSymbolicRecord.