dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

Facade APIs returning List with defined type arguments fail at runtime #33537

Open kevmoo opened 6 years ago

kevmoo commented 6 years ago
@JS("QuerySnapshot")
abstract class QuerySnapshotJsImpl {
  external List<DocumentChangeJsImpl> get docChanges;
}

This code causes an error at runtime - https://github.com/firebase/firebase-dart/issues/177

 List<DocumentChange> get docChanges =>
      jsObject.docChanges.map(DocumentChange.getInstance).toList();

The work-around is to explicitly type the method as taking a dynamic.

  List<DocumentChange> get docChanges => jsObject.docChanges
      .map((dynamic e) => DocumentChange.getInstance(e))
      .toList();

Desired: If a List/Map is returned from a facade API, it should be reified with the annotated type

matanlurey commented 6 years ago

I have an example of this in my repo: https://github.com/matanlurey/dart_js_interop#creating-a-wrapper-class

It's unlikely we'll make improvements in JS interop like this in the short-term, as its very complex. A mutable list will need to be casted, not copied, as you have above.