Skip to content

[dart:jsinterop] Allow function types in function declarations. #59672

@ykmnkmi

Description

@ykmnkmi

Current:

import 'dart:js_interop';

typedef EventDispatcher<T> = bool Function(
  String type,
  T detail, {
  bool cancelable,
});

@anonymous
extension type _EventDispatcherOptions._(JSObject _) implements JSObject {
  external factory _EventDispatcherOptions({bool cancelable});
}

@JS('createEventDispatcher')
external JSFunction _createEventDispatcher();

EventDispatcher<T> createEventDispatcher<T>() {
  var jsFunction = _createEventDispatcher();

  return (
    String type,
    T detail, {
    bool cancelable = false,
  }) {
    var jsResult = jsFunction.callAsFunction(
      null,
      type.toJS,
      // ignore: invalid_runtime_check_with_js_interop_types
      ref(detail) as JSAny?,
      _EventDispatcherOptions(cancelable: cancelable),
    ) as JSBoolean;

    return jsResult.toDart;
  };
}

Expected:

import 'dart:js_interop';

typedef EventDispatcher<T> = bool Function(
  String type,
  T detail, {
  bool cancelable,
});

@anonymous
extension type _EventDispatcherOptions._(JSObject _) implements JSObject {
  external factory _EventDispatcherOptions({bool cancelable});
}

typedef _JSEventDispatcher<T> = bool Function(
  String type,
  ExternalDartReference<T> detail, [
  _EventDispatcherOptions options,
]);

@JS('createEventDispatcher')
external _JSEventDispatcher _createEventDispatcher();

EventDispatcher<T> createEventDispatcher<T>() {
  var jsFunction = _createEventDispatcher();

  return (
    String type,
    T detail, {
    bool cancelable = false,
  }) {
    return jsFunction(
      type,
      // ignore: invalid_runtime_check_with_js_interop_types
      ref(detail),
      _EventDispatcherOptions(cancelable: cancelable),
    );
  };
}

PS: I miss and pressed Enter while editing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.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