Open navaronbracke opened 1 year ago
I think this is a feature request? Specifically to make DateTime
compatible with JavaScript's Date
type?
That example naturally won't work since DateTime
is a Dart class and not a JS interop class. I'm surprised that it printed anything as the cast to DateTime
should have failed.
I think there are several ways to do this (one of which you mention) and considering the effort it would take for this class to be interpreted differently, I'm not sure we'll fulfill this request. In the future, I believe there will be a Date
interface exposed through package:web
as well if writing the interface is cumbersome (which is reasonable, considering the number of members in the class).
I think this is a feature request? Specifically to make
DateTime
compatible with JavaScript'sDate
type?
Yes, that's exactly what I meant. The type system does give unexpected results with external
that claims to return one type, but in reality it can't return said type. Another example is external List<int> get foo;
with JS interop. The compiler allows this (even though if some list is clearly a @JS()
type, it shouldn't).
Like generics, I believe our compilers can't determine the return type of the function since it wasn't created in Dart, so they might be just playing it safe here. If we did some kind of cast after the call with date
, that fails, but that's not very ergonomic. Instead of a DateTime Function()
, if you made the return type a DateTime
and returned a Date
, that will also fail correctly. IIRC, you can use List
because it's a JSArray
under the hood.
For what it's worth, the generics issue (and similarly, this issue) is something we're addressing when we're adding a separate type hierarchy for JavaScript types. Instead of a List
or Function
, external APIs should eventually use JSArray
or JSFunction
.
Static interop currently works for primitives, lists and JS Objects.
However, it does not work for Dart's
DateTime
type.Consider the following code sample:
This produces the following log:
Since we already get int / double from DDC/Dart2js (despite JS only having num), can we do something similar for
DateTime
? I'd rather avoid having to write a static interop definition forDate
itself. A naive way would be to call.toISOString()
and redirect that toDateTime.parse()
.Dart SDK version: 3.0.0-290.3.beta (beta) (Tue Mar 21 16:51:50 2023 +0000) on "macos_x64"