Import the class of an @instanceOf type that has @module
Why
@module on an @instanceOf type was silently ignored (#9239): the class check read a global of that name, which throws a ReferenceError when the class is only exported by a module. @scope was ignored the same way.
A class is now identified by its path and its module. The check imports the class as the value of a @module external is imported: a named export, "default", or a property of an export such as "Addons.Line2".
modifiedJS class check
@instanceOf("Object3D") type object3D@module("three") @instanceOf("Mesh") type mesh = private object3D @unboxedtype shape = Mesh(mesh) | Other(object3D) let isMesh = (s: shape) => switch s { | Mesh(_) => true | Other(_) => false }
js
Before
After
let Three = require("three"); function isMesh(s) {function isMesh(s) { return s instanceof Mesh; return s instanceof Three.Mesh;}}
modifiedRepresentation class identity
representation
Before
After
@instanceOf("Mesh") ↦ global Mesh@instanceOf("Mesh") ↦ global Mesh@module("three") @instanceOf("Mesh") ↦ global Mesh @module("three") @instanceOf("Mesh") ↦ export Mesh of "three"@module("./Widget.js") @instanceOf("default") ↦ default export of "./Widget.js"
notethe two Mesh classes are different: an untagged variant may have a case for each, and an interface must state the same module
addedDiagnostic @module without @instanceOf
@module("three") type meshdiagnostic
@module on a type declaration names the module a class is imported from, so it needs @instanceOf too, such as @module("three") @instanceOf("Mesh").addedDiagnostic @scope on a type
@scope("THREE") @instanceOf("Mesh") type meshdiagnostic
@scope has no effect on a type declaration. Write the class's path in @instanceOf instead: @instanceOf("THREE.Mesh").modifiedDiagnostic interface with another module
module Three: { @instanceOf("Mesh") type mesh} = { @module("three") @instanceOf("Mesh") type mesh}
diagnostic
Their runtime representations differ: Actual declaration: instances of Mesh from "three" Expected declaration: instances of Mesh
Fixes #9239
Review
- Why an error for @scope instead of supporting it? A dotted path in @instanceOf already says the same, @instanceOf("THREE.Mesh"), so one spelling is enough.
- How is a relative module path emitted? As written, like the module of a @module external.