Closed arnaud-secondlayer closed 1 week ago
Summary: The user is encountering a build error when using js_interop
's isA
method with a generic type parameter constrained to web.Element
. The error message indicates that the type argument T
must be an interop ExtensionType
, but the user is unable to find a solution.
Thanks so much for reaching out. This is a great question!
cc @srujzs
Unfortunately, I don't believe this pattern can be supported.
The way the isA
method works, is through a static compiler transformation that needs to see explicitly the actual type passed to it. For example, the list of cases mentioned in the isA API docs are all resolved statically:
e.isA<JSBoolean>() -- becomes --> typeof e == 'boolean'
e.isA<DivElement>() -- becomes --> instanceof e == 'HTMLDivElement'
...
The pattern you show, where isA
gets wrapped in a generic method, makes the instantiation of T
happen later at runtime, and as a result the compiler no longer knows what to generate. Note that most information about interop types is erased during compilation, so there is no runtime alternative to achieve the same goal.
One small action item we can do here is to improve the error message and to add a note about this in the API docs.
I'm creating a generic function that leverages
isA
and get a build error when building to javascript. I'm not able to find a solution.I have the following script:
There is the build error with
dart compile js a.dart
: