Open parlough opened 1 day ago
Similar issue here: https://github.com/dart-lang/sdk/issues/54138
I avoided adding this because I was a little worried users might do both an isA
and an asA
e.g.
if (node.isA<HTMLAnchorElement>()) {
var anchor = node.asA<HTMLAnchorElement>();
}
leading to duplicate interop type-checks. That being said, I do like the one-liner of node.asA<HTMLAnchorElement>() case final anchor?
, so I'm torn. :)
Also, I actually would want this to throw if the type is wrong. Is there a reason we would not want this?
With
isA
, I always have to end up casting to the desired type as well if I want to use members on the specifiedT
type, as we can't rely on Dart's type promotion.I've found myself wanting to add an extension method like:
So I can easily store the result without casting separately within the if.
Yes I can add this myself, but I feel this is a pretty common case, so some version of it might be useful in
dart:js_interop
(if it makes sense).tryAsA
could also work as a name if it's desired to distinguish from Dart'sas
which can throw.