Open
Description
A feature of the Typings package is that it makes predicate (type guards) in TS (function isFoo(arg) : arg is Foo;
) a method that returns the object promoted instead of a bool.
While creating it I thought that we may have what is needed to make a type check system for js interop:
- For js classes we can do
instanceof
to check - For js interfaces we can execute the type guards when available
We could have an argument in the @JS
or @staticInterop
annotation that would map the types to the checks.
Something like
@JS()
class Foo {
}
@JS(
types: {
Foo: 'isFoo' // interface type guard
},
classCheck: 'isOf' // in the case of real class, we can make generic checks with instanceof
)
class Bar {
bool isFoo();
}
Then when a type check is used (bar is Foo
) the dart analyzer and compiler would follow some logic like in:
// bar is Foo
if (statically bar is DartType) {
// continue to the original dart type check system
} else if (statically bar is JsInterop) {
if (statically bar.isJsClassInstance) {
// check the type by calling bar.isOf(Foo)
} else if (statically bar.hasFooCheck) {
// check the type by calling bar.isFoo()
} else {
throw 'There is no type check for Bar to Foo';
}
}