-
Notifications
You must be signed in to change notification settings - Fork 45
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 |
| /// 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)?
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'd worry that this would make it too easy to hide the implicit cast. As a user and an API designer, I'd rather have it be explicit when I'm moving from type-definition-guaranteed types to expression-level-asserted types.
If this is a performance concern for JSRecord.pairs, I'd rather address that by making it directly invoke the JS Object.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.
I think the cast would still be explicit within the method, but maybe I don't fully understand
I'd rather have it be explicit when I'm moving from type-definition-guaranteed types to expression-level-asserted types.
Maybe you're saying you'd rather have the user downcast as needed? To be clear, I'm imagining something like:
List<(String, T)> entries<T extends JSAny?>() {
return [
for (var entry in _entries(this).toDart)
((entry[0] as JSString).toDart, entry[1] as T),
];
}If one of the tuple members wasn't a T, we'd see a cast failure from the cast.
We could also change the type of _entries and I believe entry[1] will be a cast failure at that point.
|
/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 |
|
Okay, I think the implementation as is looks good. When we have a I think all that's remaining here are some of my review comments and we're good to go. |
Oops, guess I missed these! Done. |
srujzs
left a comment
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.
Thanks Natalie!
| /// 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.
I think the cast would still be explicit within the method, but maybe I don't fully understand
I'd rather have it be explicit when I'm moving from type-definition-guaranteed types to expression-level-asserted types.
Maybe you're saying you'd rather have the user downcast as needed? To be clear, I'm imagining something like:
List<(String, T)> entries<T extends JSAny?>() {
return [
for (var entry in _entries(this).toDart)
((entry[0] as JSString).toDart, entry[1] as T),
];
}If one of the tuple members wasn't a T, we'd see a cast failure from the cast.
We could also change the type of _entries and I believe entry[1] will be a cast failure at that point.
This doesn't include JSObject extensions that require types that
aren't defined yet, like JSSymbolicRecord.