dart-archive / js_facade_gen

Generates package:js Javascript interop facades for arbitrary TypeScript libraries
Apache License 2.0
161 stars 29 forks source link

Use extension methods to avoid invalid override errors #77

Closed derekxu16 closed 4 years ago

derekxu16 commented 4 years ago

In TypeScript, the any type is a valid override for every type. This occurs in a few places in the DOM. In the example below, BeforeUnloadEvent.returnValue cannot be declared within BeforeUnloadEvent, otherwise it will be an invalid override. If we expose it through an extension method instead, it circumvents the error.

@JS()
library X;

import "package:js/js.dart";

@JS()
abstract class Event {
  external bool get returnValue;
  external set returnValue(bool v);
}

@JS()
abstract class BeforeUnloadEvent implements Event {}

@JS()
abstract class _BeforeUnloadEvent {
  external dynamic get returnValue;
  external set returnValue(dynamic v);
}

extension BeforeUnloadEventExtensions on BeforeUnloadEvent {
  dynamic get returnValue {
    final Object t = this;
    final _BeforeUnloadEvent tt = t;
    return tt.returnValue();
  }
}
derekxu16 commented 4 years ago

Actually this is probably unnecessary, changing dynamic to Object makes it valid.