Semantic Diff

#9432 Coerce class types explicitly across declarations

1 commit

17874c042551ecd4977b687bb025c7130ba7d0b3..pr-9432

7964eeafe · jfrolich + Jaap Frolich · Nov 16, 2026, 20:23 UTC

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
Before
p1 = p2
p1 <: p2
(Same path)
After
class(p2) ∈ classes(p1)
p1 <: p2
(Class)
class(p): the class that the declaration of p itself states with @instanceOf, with its @module
classes(p): class(p), or the class p abbreviates, with its declared superclasses

removedDiagnostic 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
Before each line compiled alone
After
Type ReactDOM.element is not a subtype of WebAPI.elementType WebAPI.input is not a subtype of ReactDOM.nodeType array<ReactDOM.element> is not a subtype of array<WebAPI.element>Type ReactDOM.element is not compatible with type WebAPI.elementnone
js
After
function a(el) {  return el;}

modifiedDiagnostic a class that is not a superclass

let d = (el: ReactDOM.element) => (el :> WebAPI.input)
diagnostic
Before
After
Type ReactDOM.element is not a subtype of WebAPI.inputType ReactDOM.element is not a subtype of WebAPI.input ReactDOM.element is the class Element, a subclass of Node. WebAPI.input is the class HTMLInputElement. A class type coerces to its own class and to the superclasses it declares.

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, so type 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.