Skip to content

Commit e2699cd

Browse files
DZakhcristianoc
authored andcommitted
Clean up genType supported types page a little bit
1 parent f51504e commit e2699cd

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pages/docs/gentype/latest/supported-types.mdx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ The JS values are identical: there is no conversion unless the argument type nee
3737

3838
## Records
3939

40-
ReScript record values of type e.g. `{x:int}` such as `{x:0}`, `{x:1}`, `{x:2}`, are exported to JS object values `{x:0}`, `{x:1}`, `{x:2}`. This requires a change of runtime representation from arrays to objects.
41-
So they are exported to JS values of type `{x:number}`.
40+
ReScript record values of type e.g. `{x: int}` such as `{x: 0}`, `{x: 1}`, are exported to JS values of type `{x: number}` without runtime conversion.
4241

4342
Since records are immutable by default, their fields will be exported to readonly property types in Flow/TS. Mutable fields are specified in ReScript by e.g. `{mutable mutableField: string}`.
4443

45-
The `@genType.as` annotation can be used to change the name of a field on the JS side of things. So e.g. `{[@genType.as "y"] x:int}` is exported as JS type `{y:int}`.
44+
The `@as` annotation can be used to change the name of a field on the JS side of things. So e.g. `{@as("y") x: int}` is exported as JS type `{y: number}`.
4645

4746
If one field of the ReScript record has option type, this is exported to an optional JS field. So for example ReScript type `{x: option<int>}` is exported as JS type `{x?: number}`.
4847

@@ -67,13 +66,13 @@ Ordinary variants (with capitalized cases, e.g. ``` | A | B(int) ```) and polymo
6766
Variants can have an *unboxed*, or a *boxed* representation. The unboxed representation is used when there is at most one case with a payload, and that payload has object type; otherwise, a boxed representation is used. Object types are arrays, objects, records and tuples.
6867

6968
Variants without payloads are essentially sequences of identifiers.
70-
E.g. type ``[@genType] type days = | Monday | Tuesday``.
69+
E.g. type ``@genType type days = Monday | Tuesday``.
7170
The corresponding JS representation is `"Monday"`, `"Tuesday"`.
72-
Similarly, polymorphic variant type ``[@genType] type days = [ | `Monday | `Tuesday ] `` has the same JS representation.
71+
Similarly, polymorphic variant type ``@genType type days = [#Monday | #Tuesday]`` has the same JS representation.
7372

7473

7574
When at most one variant case has a payload, and if the payload is of object type, e.g.
76-
``` [ | Unnamed | Named({. "name": string, "surname": string}) ] ```
75+
```Unnamed | Named({. "name": string, "surname": string})```
7776
then the representation is unboxed: JS values are e.g. `"Unnamed"` and
7877
`{name: "hello", surname: "world"}`. Similarly for polymorphic variants.
7978
Note that this unboxed representation does not use the label `"Named"` of the variant case with payload, because that value is distinguished from the other payload-less cases by its type: an object.
@@ -83,8 +82,8 @@ For example, type ```| A | B(int) | C(string)``` has values such as ```"A"``` an
8382
```{tag: "B", value: 42}``` and ```{tag: "C", value: "hello"}```.
8483
Polymorhphic variants are treated similarly. Notice that payloads for polymorphic variants are always unary: ``` `Pair(int,int) ``` has a single payload of type `(int,int)`. Instead, ordinary variants distinguish between unary ``` Pair((int,int)) ``` and binary ``` Pair(int,int) ``` payloads. All those cases are represented in JS as ```{tag: "Pair", value: [3, 4]}```, and the conversion functions take care of the different ReScript representations.
8584

86-
The `@genType.as` annotation can be used to modify the name emitted for a variant case on the JS side. So e.g. ``` | [@genType.as "Arenamed"] A``` exports ReScript value `` A `` to JS value `"Arenamed"`.
87-
Boolean/integer/float constants can be expressed as ``` | [@genType.as true] True ``` and ``` | [@genType.as 20] Twenty ``` and ``` | [@genType.as 0.5] Half ```. Similarly for polymorphic variants.
85+
The `@genType.as` annotation can be used to modify the name emitted for a variant case on the JS side. So e.g. ``` | @genType.as("Arenamed") A``` exports ReScript value `` A `` to JS value `"Arenamed"`.
86+
Boolean/integer/float constants can be expressed as ``` | @genType.as(true) True ``` and ``` | @genType.as(20) Twenty ``` and ``` | @genType.as(0.5) Half ```. Similarly for polymorphic variants.
8887
The `@genType.as` annotation can also be used on variants with payloads to determine what appears in `{ tag: ... }`.
8988

9089
For more examples, see [Variants.res](https://github.com/reason-association/genType/tree/master/examples/typescript-react-example/src/Variants.res) and [VariantsWithPayload.res](https://github.com/reason-association/genType/tree/master/examples/typescript-react-example/src/VariantsWithPayload.res).
@@ -110,9 +109,9 @@ In case of mixed named and unnamed arguments, consecutive named arguments form s
110109

111110
Function components are exported and imported exactly like normal functions. For example:
112111

113-
```reason
114-
[@genType]
115-
[@react.component]
112+
```rescript
113+
@genType
114+
@react.component
116115
let make = (~name) => React.string(name);
117116
```
118117

0 commit comments

Comments
 (0)