Skip to content

Commit e97505b

Browse files
committed
ReScriptify genType supported-types docs
1 parent 3750f78 commit e97505b

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

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

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,54 @@
22

33
<Intro>
44

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.
66

77
</Intro>
88

99
## Int
1010

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`.
1212

1313
## Float
1414

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`.
1616

1717
## String
1818

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`.
2020

2121
## Optionals
2222

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`.
2424
The JS values are unboxed, and `null`/`undefined` are conflated.
2525
So the option type is exported to JS type `null` or `undefined` or `number`.
2626

2727
## Nullables
2828

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`.
3030
The JS values are identical: there is no conversion unless the argument type needs conversion.
3131

3232
## Records
3333

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.
3535
So they are exported to JS values of type `{x:number}`.
3636

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}`.
3838

3939
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}`.
4040

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}`.
4242

4343
## Objects
4444

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}`.
4646
A conversion is required only when the type of some field requires conversions.
4747

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 }`.
4949

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.
5151

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):
5353

5454
```
5555
Remove trailing "__" if present.
@@ -58,8 +58,8 @@ Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
5858

5959
## Tuples
6060

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.
6363

6464
## Variants
6565

@@ -82,9 +82,9 @@ Note that this unboxed representation does not use the label `"Named"` of the va
8282
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}```.
8383
For example, type ```| A | B(int) | C(string)``` has values such as ```"A"``` and
8484
```{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.
8686

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"`.
8888
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.
8989
The `@genType.as` annotation can also be used on variants with payloads to determine what appears in `{ tag: ... }`.
9090

@@ -94,18 +94,18 @@ For more examples, see [Variants.res](examples/typescript-react-example/src/Vari
9494

9595
## Arrays
9696

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.
9898

99-
Immutable arrays are supported with the additional Reason library
99+
Immutable arrays are supported with the additional ReScript library
100100
[ImmutableArray.res/.resi](examples/typescript-react-example/src/ImmutableArray.resi), which currently needs to be added to your project.
101101
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`.
102102

103103
## Functions and Function Components
104104

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.
107107

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.
109109

110110
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`.
111111

@@ -117,7 +117,7 @@ Function components are exported and imported exactly like normal functions. For
117117
let make = (~name) => React.string(name);
118118
```
119119

120-
For renaming, named arguments follow bucklescript's mangling convention:
120+
For renaming, named arguments follow ReScript's mangling convention:
121121

122122
```
123123
Remove trailing "__" if present.
@@ -126,23 +126,23 @@ Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
126126

127127
For example:
128128

129-
```reason
130-
[@genType]
131-
let exampleFunction = (~_type) => "type: " ++ _type;
129+
```res
130+
@genType
131+
let exampleFunction = (~_type) => "type: " ++ _type
132132
```
133133

134134
## Record Components
135135

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 ...`.
137137

138138
A file can export many components by defining them in sub-modules. The toplevel component is also exported as default.
139139

140140
## Imported Types
141141

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,
143143

144-
```reason
145-
[@genType.import "./SomeFlowTypes"] type weekday;
144+
```res
145+
@genType.import("./SomeFlowTypes") type weekday
146146
```
147147

148148
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
156156

157157
## First Class Modules
158158

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.
160160
For example,
161161

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)
166172
```
167173

168174
is exported as a JS object of type
169175

170-
```reason
171-
{x: number, y: string}
176+
```res
177+
{"x": number, "y": string}
172178
```
173179

174180
Notice how the order of elements in the exported JS object is determined by the module type `MT` and not the module implementation `M`.
175181

176182
## Polymorphic Types
177183

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.
179185

180186
### Exporting Values from Polymorphic Types with Hidden Type Variables
181187

182188
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:
183189

184190
**Doesn't work**
185191

186-
```reason
187-
[@genType]
188-
let none = None;
192+
```res
193+
@genType
194+
let none = None
189195
```
190196

191197
```js
@@ -194,9 +200,9 @@ export const none: ?T1 = OptionBS.none; // Errors out as T1 is not defined
194200

195201
**Works**
196202

197-
```reason
198-
[@genType]
199-
let none = () => None;
203+
```res
204+
@genType
205+
let none = () => None
200206
```
201207

202208
```js

0 commit comments

Comments
 (0)