Skip to content

Can return value of .jsify() be non-nullable if receiver is non-nullable? #55463

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
dgreensp opened this issue Apr 12, 2024 · 2 comments
Open
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop

Comments

@dgreensp
Copy link

I've noticed that you always need a ! assertion after .jsify() to get a JSAny:

  document.body!.addEventListener(
      "click",
      (JSAny e) {
        print("click");
        console.log(e);
      }.toJS,
      {"once": true}.jsify()!);

Since extension methods on overlapping types (one more specific than another) work great in Dart, it seems like you could add this to dart:js_interop so that the result of .jsify() is non-nullable if the receiver is non-nullable:

extension ObjectUtilExtension on Object {
  JSAny jsify() => NullableObjectUtilExtension(this).jsify()!;
}

Am I missing a case where the result of .jsify() would be null for a non-null receiver?

@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 Apr 13, 2024
@srujzs srujzs added the type-question A question about expected behavior or functionality label Apr 19, 2024
@srujzs
Copy link
Contributor

srujzs commented Apr 19, 2024

I think you're right that jsify() can't return null if the input is non-nullable. There is a discussion about what to do with undefined values here: #54025 that may affect this in the future, but for now, you can indeed declare an extension method like that.

@dgreensp
Copy link
Author

@srujzs Thanks. I hope this is fixed/improved in dart:js_interop itself, because adding it to my codebase has downsides (it has to be imported, and it subtly changes the behavior of a well-known method, which could be confusing, all to save one character); I decided against it for now. Whereas if it comes "for free," it is a nice improvement.

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. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop
Projects
Status: No status
Development

No branches or pull requests

3 participants