Coerce a class type to every declaration of its class and its superclasses
Why
Two packages that bind the same JavaScript class each declare their own type for it, @instanceOf("Element") type element in one and in the other, and :> did not get from one to the other: it only follows the abbreviations a type is declared with. So every binding package depends on the one package that owns the DOM types, or its users convert with Obj.magic. The class an @instanceOf type names is what its values are at run time; it now decides what they coerce to.
modifiedTyping :> between class types, Ctype.subtype_rec
rule
p1 <: p2p1 <: p2removedDiagnostic coercions between declarations of one class
module WebAPI = { @instanceOf("Node") type node @instanceOf("Element") type element = private node @instanceOf("HTMLInputElement") type input = private element}module ReactDOM = { @instanceOf("Node") type node @instanceOf("Element") type element = private node}let a = (el: ReactDOM.element) => (el :> WebAPI.element)let b = (input: WebAPI.input) => (input :> ReactDOM.node)let c = (els: array<ReactDOM.element>) => (els :> array<WebAPI.element>)
diagnostic
js
function a(el) { return el;}
modifiedDiagnostic a class that is not a superclass
let d = (el: ReactDOM.element) => (el :> WebAPI.input)diagnostic
The fields or contents of the two declarations are not compared: each says what its module knows about the class. A class is its path and its module, so a global Mesh and the Mesh that "three" exports stay unrelated. Where a coercion needs equal types, an invariant parameter as in array<_> or a mutable field, it is checked both ways as before, which two declarations of one class pass and a subclass does not.
Review
- Can
:>now reach a type it could not before, other than a class type? No: the arm only matches two nullary type constructors whose target states a class with its own@instanceOf, sotype mine = private element, a refinement without an annotation, still accepts no coercion into it. - Do programs that compiled before change? No: the arm only adds coercions, and
:>produces the value unchanged.