You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/gentype/latest/supported-types.mdx
+10-11Lines changed: 10 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -37,12 +37,11 @@ The JS values are identical: there is no conversion unless the argument type nee
37
37
38
38
## Records
39
39
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.
42
41
43
42
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}`.
44
43
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}`.
46
45
47
46
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}`.
48
47
@@ -67,13 +66,13 @@ Ordinary variants (with capitalized cases, e.g. ``` | A | B(int) ```) and polymo
67
66
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.
68
67
69
68
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``.
71
70
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.
73
72
74
73
75
74
When at most one variant case has a payload, and if the payload is of object type, e.g.
then the representation is unboxed: JS values are e.g. `"Unnamed"` and
78
77
`{name: "hello", surname: "world"}`. Similarly for polymorphic variants.
79
78
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
83
82
```{tag: "B", value: 42}``` and ```{tag: "C", value: "hello"}```.
84
83
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.
85
84
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.astrue] True ``` and ``` | [@genType.as 20] Twenty ``` and ``` | [@genType.as0.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.
88
87
The `@genType.as` annotation can also be used on variants with payloads to determine what appears in `{ tag: ... }`.
89
88
90
89
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
110
109
111
110
Function components are exported and imported exactly like normal functions. For example:
0 commit comments