Skip to content

Type check and promotion for js interop #52030

Open
@jodinathan

Description

@jodinathan

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';
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions