Skip to content

[dart:js_interop]: Add a way to create a Dart-typed function using the JSFunction constructor.Β #60428

Open
@ykmnkmi

Description

@ykmnkmi

For example, instead of writing this:

import 'dart:js_interop';

@JS('Function')
external JSFunction _createFunction2(String first, String second, String body);

final JSFunction _add = _createFunction2('a', 'b', 'return a + b');

int add(int a, int b) {
  var result = _add.callAsFunction(null, a.toJS, b.toJS) as JSNumber;
  return result.toDartInt;
}

void main() {
  print(add(1, 2));
}

write it this way:

import 'dart:js_interop';

@JS('Function')
external JSFunction<int Function(int, int)> _createFunction2(String first, String second, String body);

final JSFunction<int Function(int, int)> add = _createFunction2('a', 'b', 'return a + b');

void main() {
  print(add(1, 2)); // If a direct call is possible.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.type-enhancementA request for a change that isn't a bugweb-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