Skip to content

Commit f72c525

Browse files
authored
tweaks to the @as decorator syntax widget docs (#1011)
1 parent a40fe48 commit f72c525

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

misc_docs/syntax/decorator_as.mdx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ summary: "This is the `@as` decorator."
66
category: "decorators"
77
---
88

9-
The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name.
9+
The `@as` decorator has multiple uses in ReScript.
10+
11+
## Change runtime name of record field
12+
The `@as` decorator can be used on record types to alias record field names to a different JavaScript attribute name.
1013

1114
This is useful to map to JavaScript attribute names that cannot be expressed in ReScript (such as keywords).
1215

@@ -52,8 +55,36 @@ var value = [
5255

5356
</CodeTab>
5457

58+
## Change the runtime representation of a variant constructor
59+
Similarily to changing the runtime name of a record field, you can change the runtime representation of a variant constructor using `@as()`. Only with variants, you have many more options for the runtime representation than for record field names:
60+
61+
<CodeTab labels={["ReScript", "JS Output"]}>
62+
63+
```res
64+
@unboxed
65+
type pet = | @as("dog") Dog | @as(1) Cat | @as(null) SomethingElse
66+
67+
let dog = Dog
68+
let cat = Cat
69+
let somethingElse = SomethingElse
70+
71+
```
72+
73+
```js
74+
let dog = "dog";
75+
76+
let cat = 1;
77+
78+
let somethingElse = null;
79+
```
80+
81+
</CodeTab>
82+
83+
Read more about the [`@as` decorator and variants](variant.md#valid-as-payloads).
84+
5585
### References
5686

5787
* [Bind Using ReScript Record](/docs/manual/latest/bind-to-js-object#bind-using-rescript-record)
5888
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better)
5989
* [Fixed Arguments](/docs/manual/latest/bind-to-js-function#fixed-arguments)
90+
* [`@as` decorator and variants](variant.md#valid-as-payloads)

0 commit comments

Comments
 (0)