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
+50-44Lines changed: 50 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,54 +2,54 @@
2
2
3
3
<Intro>
4
4
5
-
Some types and values in Reason do not map directly to JavaScript and need to be converted whenever a value crosses the boundary. This document gives an overview on how `genType`'s convertion works on different types.
5
+
Some types and values in ReScript do not map directly to JavaScript and need to be converted whenever a value crosses the boundary. This document gives an overview on how `genType`'s convertion works on different types.
6
6
7
7
</Intro>
8
8
9
9
## Int
10
10
11
-
Reason values e.g. `1`, `2`, `3` are unchanged. So they are exported to JS values of type `number`.
11
+
ReScript values e.g. `1`, `2`, `3` are unchanged. So they are exported to JS values of type `number`.
12
12
13
13
## Float
14
14
15
-
Reason values e.g. `1.0`, `2.0`, `3.0` are unchanged. So they are exported to JS values of type `number`.
15
+
ReScript values e.g. `1.0`, `2.0`, `3.0` are unchanged. So they are exported to JS values of type `number`.
16
16
17
17
## String
18
18
19
-
Reason values e.g. `"a"`, `"b"`, `"c"` are unchanged. So they are exported to JS values of type `string`.
19
+
ReScript values e.g. `"a"`, `"b"`, `"c"` are unchanged. So they are exported to JS values of type `string`.
20
20
21
21
## Optionals
22
22
23
-
Reason values of type e.g. `option(int)`, such as `None`, `Some(0)`, `Some(1)`, `Some(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`.
23
+
ReScript values of type e.g. `option(int)`, such as `None`, `Some(0)`, `Some(1)`, `Some(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`.
24
24
The JS values are unboxed, and `null`/`undefined` are conflated.
25
25
So the option type is exported to JS type `null` or `undefined` or `number`.
26
26
27
27
## Nullables
28
28
29
-
Reason values of type e.g. `Js.Nullable.t(int)`, such as `Js.Nullable.null`, `Js.Nullable.undefined`, `Js.Nullable.return(0)`, `Js.Nullable.return(1)`, `Js.Nullable.return(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`.
29
+
ReScript values of type e.g. `Js.Nullable.t(int)`, such as `Js.Nullable.null`, `Js.Nullable.undefined`, `Js.Nullable.return(0)`, `Js.Nullable.return(1)`, `Js.Nullable.return(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`.
30
30
The JS values are identical: there is no conversion unless the argument type needs conversion.
31
31
32
32
## Records
33
33
34
-
Reason 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.
34
+
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.
35
35
So they are exported to JS values of type `{x:number}`.
36
36
37
-
Since records are immutable by default, their fields will be exported to readonly property types in Flow/TS. Mutable fields are specified in Reason by e.g. `{mutable mutableField: string}`.
37
+
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}`.
38
38
39
39
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}`.
40
40
41
-
If one field of the Reason record has option type, this is exported to an optional JS field. So for example Reason type `{x: option(int)}` is exported as JS type `{x?: number}`.
41
+
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}`.
42
42
43
43
## Objects
44
44
45
-
Reason object values of type e.g. `{. "x":int}` such as `{"x": 0}`, `{"x": 1}`, `{"x": 2}`, are exported as identical JS object values `{x:0}`, `{x:1}`, `{x:2}`. This requires no conversion. So they are exported to JS values of type `{x:number}`.
45
+
ReScript object values of type e.g. `{. "x":int}` such as `{"x": 0}`, `{"x": 1}`, `{"x": 2}`, are exported as identical JS object values `{x:0}`, `{x:1}`, `{x:2}`. This requires no conversion. So they are exported to JS values of type `{x:number}`.
46
46
A conversion is required only when the type of some field requires conversions.
47
47
48
-
Since objects are immutable by default, their fields will be exported to readonly property types in Flow/TS. Mutable fields are specified in Reason by e.g. `{. [@bs.set] "mutableField": string }`.
48
+
Since objects 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. `{. [@bs.set] "mutableField": string }`.
49
49
50
-
It is possible to mix object and option types, so for example the Reason type `{. "x":int, "y":option(string)}` exports to JS type `{x:number, ?y: string}`, requires no conversion, and allows option pattern matching on the Reason side.
50
+
It is possible to mix object and option types, so for example the ReScript type `{. "x":int, "y":option(string)}` exports to JS type `{x:number, ?y: string}`, requires no conversion, and allows option pattern matching on the ReScript side.
51
51
52
-
Object field names follow bucklescript's mangling convention (so e.g. `_type` in Reason represents `type` in JS):
52
+
Object field names follow ReScript's mangling convention (so e.g. `_type` in ReScript represents `type` in JS):
53
53
54
54
```
55
55
Remove trailing "__" if present.
@@ -58,8 +58,8 @@ Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
58
58
59
59
## Tuples
60
60
61
-
Reason tuple values of type e.g. `(int, string)` are exported as identical JS values of type `[number, string]`. This requires no conversion, unless one of types of the tuple items does.
62
-
While the type of Reason tuples is immutable, there's currently no mature enforcement in TS/Flow, so they're currenty exported to mutable tuples.
61
+
ReScript tuple values of type e.g. `(int, string)` are exported as identical JS values of type `[number, string]`. This requires no conversion, unless one of types of the tuple items does.
62
+
While the type of ReScript tuples is immutable, there's currently no mature enforcement in TS/Flow, so they're currenty exported to mutable tuples.
63
63
64
64
## Variants
65
65
@@ -82,9 +82,9 @@ Note that this unboxed representation does not use the label `"Named"` of the va
82
82
If there is more than one case with payload, or if the single payload has not type object, a boxed representation is used. The boxed representation has shape ```{tag: "someTag", value: someValue}```.
83
83
For example, type ```| A | B(int) | C(string)``` has values such as ```"A"``` and
84
84
```{tag: "B", value: 42}``` and ```{tag: "C", value: "hello"}```.
85
-
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 Reason representations.
85
+
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.
86
86
87
-
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 Reason value `` A `` to JS value `"Arenamed"`.
87
+
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"`.
88
88
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.
89
89
The `@genType.as` annotation can also be used on variants with payloads to determine what appears in `{ tag: ... }`.
90
90
@@ -94,18 +94,18 @@ For more examples, see [Variants.res](examples/typescript-react-example/src/Vari
94
94
95
95
## Arrays
96
96
97
-
Arrays with elements of Reason type `t` are exported to JS arrays with elements of the corresponding JS type. If a conversion is required, a copy of the array is performed.
97
+
Arrays with elements of ReScript type `t` are exported to JS arrays with elements of the corresponding JS type. If a conversion is required, a copy of the array is performed.
98
98
99
-
Immutable arrays are supported with the additional Reason library
99
+
Immutable arrays are supported with the additional ReScript library
100
100
[ImmutableArray.res/.resi](examples/typescript-react-example/src/ImmutableArray.resi), which currently needs to be added to your project.
101
101
The type `ImmutableArray.t(+'a)` is covariant, and is mapped to readonly array types in TS/Flow. As opposed to TS/Flow, `ImmutableArray.t` does not allow casting in either direction with normal arrays. Instead, a copy must be performed using `fromArray` and `toArray`.
102
102
103
103
## Functions and Function Components
104
104
105
-
Reason functions are exported as JS functions of the corresponding type.
106
-
So for example a Reason function `foo : int => int` is exported as a JS function from numbers to numbers.
105
+
ReScript functions are exported as JS functions of the corresponding type.
106
+
So for example a ReScript function `foo : int => int` is exported as a JS function from numbers to numbers.
107
107
108
-
If named arguments are present in the Reason type, they are grouped and exported as JS objects. For example `foo : (~x:int, ~y:int) => int` is exported as a JS function from objects of type `{x:number, y:number}` to numbers.
108
+
If named arguments are present in the ReScript type, they are grouped and exported as JS objects. For example `foo : (~x:int, ~y:int) => int` is exported as a JS function from objects of type `{x:number, y:number}` to numbers.
109
109
110
110
In case of mixed named and unnamed arguments, consecutive named arguments form separate groups. So e.g. `foo : (int, ~x:int, ~y:int, int, ~z:int) => int` is exported to a JS function of type `(number, {x:number, y:number}, number, {z:number}) => number`.
111
111
@@ -117,7 +117,7 @@ Function components are exported and imported exactly like normal functions. For
117
117
let make = (~name) => React.string(name);
118
118
```
119
119
120
-
For renaming, named arguments follow bucklescript's mangling convention:
120
+
For renaming, named arguments follow ReScript's mangling convention:
121
121
122
122
```
123
123
Remove trailing "__" if present.
@@ -126,23 +126,23 @@ Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
126
126
127
127
For example:
128
128
129
-
```reason
130
-
[@genType]
131
-
let exampleFunction = (~_type) => "type: " ++ _type;
129
+
```res
130
+
@genType
131
+
let exampleFunction = (~_type) => "type: " ++ _type
132
132
```
133
133
134
134
## Record Components
135
135
136
-
ReasonReact record components with props of Reason types `t1`, `t2`, `t3` are exported as reactjs components with props of the JS types corresponding to `t1`, `t2`, `t3`. The annotation is on the `make` function: `[@genType] let make ...`.
136
+
ReasonReact record components with props of ReScript types `t1`, `t2`, `t3` are exported as reactjs components with props of the JS types corresponding to `t1`, `t2`, `t3`. The annotation is on the `make` function: `[@genType] let make ...`.
137
137
138
138
A file can export many components by defining them in sub-modules. The toplevel component is also exported as default.
139
139
140
140
## Imported Types
141
141
142
-
It's possible to import an existing TS/Flow type as an opaque type in Reason. For example,
142
+
It's possible to import an existing TS/Flow type as an opaque type in ReScript. For example,
143
143
144
-
```reason
145
-
[@genType.import"./SomeFlowTypes"] type weekday;
144
+
```res
145
+
@genType.import("./SomeFlowTypes") type weekday
146
146
```
147
147
148
148
defines a type which maps to `weekday` in `SomeFlowTypes.js`.
@@ -156,36 +156,42 @@ See for example [Types.res](examples/typescript-react-example/src/nested/Types.r
156
156
157
157
## First Class Modules
158
158
159
-
Reason first class modules are converted from their array Reason runtime representation to JS Object types.
159
+
ReScript first class modules are converted from their array ReScript runtime representation to JS Object types.
160
160
For example,
161
161
162
-
```reason
163
-
module type MT = { let x: int; let y: string; };
164
-
module M = { let y = "abc"; let x = 42; };
165
-
[@genType] let firstClassModule: module MT = (module M);
162
+
```res
163
+
module type MT = {
164
+
let x: int
165
+
let y: string
166
+
}
167
+
module M = {
168
+
let y = "abc"
169
+
let x = 42
170
+
}
171
+
export firstClassModule: module(MT) = module(M)
166
172
```
167
173
168
174
is exported as a JS object of type
169
175
170
-
```reason
171
-
{x: number, y: string}
176
+
```res
177
+
{"x": number, "y": string}
172
178
```
173
179
174
180
Notice how the order of elements in the exported JS object is determined by the module type `MT` and not the module implementation `M`.
175
181
176
182
## Polymorphic Types
177
183
178
-
If a Reason type contains a type variable, the corresponding value is not converted. In other words, the conversion is the identity function. For example, a Reason function of type `{payload: 'a} => 'a` must treat the value of the payload as a black box, as a consequence of parametric polymorphism. If a typed back-end is used, the reason type is converted to the corresponding generic type.
184
+
If a ReScript type contains a type variable, the corresponding value is not converted. In other words, the conversion is the identity function. For example, a ReScript function of type `{payload: 'a} => 'a` must treat the value of the payload as a black box, as a consequence of parametric polymorphism. If a typed back-end is used, the ReScript type is converted to the corresponding generic type.
179
185
180
186
### Exporting Values from Polymorphic Types with Hidden Type Variables
181
187
182
188
For cases when a value that contains a hidden type variable needs to be converted, a function can be used to produce the appropriate output:
183
189
184
190
**Doesn't work**
185
191
186
-
```reason
187
-
[@genType]
188
-
let none = None;
192
+
```res
193
+
@genType
194
+
let none = None
189
195
```
190
196
191
197
```js
@@ -194,9 +200,9 @@ export const none: ?T1 = OptionBS.none; // Errors out as T1 is not defined
0 commit comments