Syntax Lookup
:>
The :> operator may be used to convert a polymorphic variant to a string or int, or convert an object to a type with a subset of its fields.
Since ReScript 11.0.0 coercion also works for converting
from
inttofloatfrom record to record with the same field(s) or when record A is a subtype of record B
from
@unboxedvariant with only strings tostringfrom
stringto@unboxedvariant that have a catch-all unboxedstringcasefrom variant to variant when applicable
from variant to
string/int/floatwhen applicablefor invariant type arguments such as array payloads when the runtime representation is guaranteed to be exactly the same
Since ReScript 12, coercion also supports:
a closed polymorphic variant to a regular variant when all possible source constructors have matching target constructors
resolving type variables from the surrounding expression while checking the coercion
Example 1
Example 2
Example 3
Example 4
Polymorphic Variant to Regular Variant
REStype command = [#Start | #Stop]
type event = Start | Stop | Pause
let command: command = #Start
let event: event = (command :> event)
Inferred Source Type
REStype userId = private string
let inspect = (_: userId) => ()
let unwrap = value => {
inspect(value)
(value :> string)
}
The call to inspect determines the otherwise-unannotated value parameter's type, and the coercion is checked with that inferred type.