#9413 Coerce a class type to every declaration of its class
3 commits
0bd4be263fdce99f896097d95ef17132c73bcccf..pr-9413
bfce750fd · 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
removedDiagnosticcoercions between declarations of one class
moduleWebAPI = {@instanceOf("Node") type node@instanceOf("Element") type element = private node@instanceOf("HTMLInputElement") type input = private element}moduleReactDOM = {@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;}
modifiedDiagnostica 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.inputReactDOM.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.
6231f0bdb · jfrolich + Jaap Frolich · Nov 16, 2026, 20:24 UTC
Accept another declaration of a class at an argument without :>
Why
With the previous commit WebAPI.focus((el :> WebAPI.element)) compiles for an el: ReactDOM.element, but each call into another package's bindings still needs the written coercion, also for every JSX prop. Where the compiler knows the type a site expects, it can make the coercion itself: the two types name one class, and the value is the same.
modifiedTypingan argument at a site that declares its type, Typecore.unify_exp_at_argument
rule
Before
Γ ⊢ e : ττ = τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Argument)
After
Γ ⊢ e : ττ = τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Argument)
Γ ⊢ e : ττ ≠ τ'τ ≤class τ'
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
(Class argument)
τ ≤class τ': τ is a class type of the class of τ' or of a subclass, or τ and τ' are equal up to two declarations of one class inside them
removedDiagnosticanother declaration of the parameter's class
moduleWebAPI = {@instanceOf("Element") type element@instanceOf("HTMLInputElement") type input = private element@sendexternal focus: element => unit = "focus"}moduleReactDOM = {@instanceOf("Element") type elementtype props = {container: element}}let a = (el: ReactDOM.element) => WebAPI.focus(el)let b = (input: WebAPI.input): ReactDOM.props => {container: input}
diagnostic
Before each line compiled alone
After
This has type: ReactDOM.elementBut this function argument is expecting: WebAPI.elementThis has type: WebAPI.inputBut the record field container is expected to have type: ReactDOM.elementnone
js
After
function a(el) { el.focus();}function b(input) {return { container: input };}
modifiedDiagnosticwhere :> stays written
let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : blet take = (_: option<ReactDOM.element>) => ()let d = (input: option<WebAPI.input>) => take(input)
diagnostic
Before each compiled alone, the second with the parent commit's compiler
After
This has type: WebAPI.elementBut this ternary is expected to return: ReactDOM.elementThis has type: option<WebAPI.input>But this function argument is expecting: option<ReactDOM.element>The incompatible parts: WebAPI.input vs ReactDOM.elementPossible solutions:- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)unchanged
The sites are the parameters of a function (labelled, optional and piped arguments too), the fields of a record being built, which includes JSX props, and constructor payloads: their expected type is declared. In first ? a : b the expected type of b is the type of a, so accepting it would make the type of the result, and with a subclass whether it compiles, depend on the order of the branches; an assignment to a mutable field keeps :> too. Inside another type only the same class is accepted (Same_class steps of subtype_rec): option<input> for option<element> is a subclass coercion that :> states. The argument gets the expected type, as (e :> t) would give it.
Review
Does a program that compiled before get other types? No: the rule is only tried where the argument's type failed to unify with the expected one, which was an error.
Are other errors printed differently? No: when the class check fails, it restores the type state it changed and the original clash is raised. The super_errors expectations are unchanged.
db3017f2d · jfrolich + Jaap Frolich · Nov 16, 2026, 20:24 UTC
Document that class types coerce by their class
The rules for @instanceOf types in UntaggedVariants.md and the @instanceOf completion doc now say what a class type coerces to and where no :> is needed, with the errors and their fixtures. The changelog fragment describes the feature of the two commits before.