diff --git a/misc_docs/syntax/operators_type_coercion.mdx b/misc_docs/syntax/operators_type_coercion.mdx new file mode 100644 index 000000000..bbf7afef7 --- /dev/null +++ b/misc_docs/syntax/operators_type_coercion.mdx @@ -0,0 +1,69 @@ +--- +id: "type-coercion" +keywords: ["operator", "type", "coercion", "polymorphic", "variant", "polyvar", "object"] +name: ":>" +summary: "This is the `type coercion` operator." +category: "operators" +--- + +The `:>` operator may be used to convert a polymorphic variant to a `string` or `int`, or convert an [object](/docs/manual/latest/object) to a type with a subset of its fields. + +### Example 1 + + + +```res +type color = [#Red | #Green | #Blue] +let color: color = #Red +let message = "The color is " ++ (color :> string) +``` + +```js +var message = "The color is Red"; +``` + + + +### Example 2 + + + +```res +type bit = [#0 | #1] +let bit: bit = #1 +let value = (bit :> int) +``` + +```js +var bit = 1; +var value = 1; +``` + + + +### Example 3 + + + +```res +type person = {"id": int, "name": string} +type name = {"name": string} +let person = {"id": 10, "name": "Gideon"} +let name = (person :> name) +``` + +```js +var person = { + id: 10, + name: "Gideon", +}; + +var name = person; +``` + + + + +### References + +* [Polymorphic Variant Coercion](/docs/manual/latest/polymorphic-variant#coercion)