Open
Description
import 'dart:js_interop';
void main() {
print((0.toJS as JSBoolean).toDart);
print((0.toJS as JSString).toDart);
print((true.toJS as JSNumber).toDartDouble);
print((true.toJS as JSNumber).toDartInt);
print((true.toJS as JSString).toDart);
print((''.toJS as JSNumber).toDartDouble);
print((''.toJS as JSNumber).toDartInt);
print((''.toJS as JSBoolean).toDart);
}
outputs:
false
0
1.0
1
true
0.0
0
false
on dart2wasm. It throws on the JS backends wherever the casts are due to the reified types of JS types. The conversions are implemented in dart2wasm to use the inline JS<>
helper, which simply tries to coerce the underlying extern ref into the Dart type (bool
, String
, etc.).
Note that if we used external
getters with Dart primitive types instead e.g. external bool get y;
where y
was not a boolean JS value, an error is thrown.
For consistency and less confusion, we should probably throw a TypeError
if the value is not the JS type it claims to be when converting. This way, users only pay the cost of type-checking when they try to internalize the JS value.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
No status