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
> **Note:** In ReScript >= v7 [records are already compiled to JS
111
-
> objects](bind-to-js-object#bind-to-record-like-js-objects). `@deriving(jsConverter)` is therefore
112
-
> obsolete and will generate a no-op function for compatibility instead.
113
-
114
-
Use `@deriving(jsConverter)` on a record type to create convertion functions between records / JS object runtime values.
115
-
116
-
117
-
```res
118
-
@deriving(jsConverter)
119
-
type coordinates = {
120
-
x: int,
121
-
y: int
122
-
};
123
-
```
124
-
125
-
Generates 2 functions of the following types:
126
-
127
-
```res
128
-
let coordinatesToJs: coordinates => {"x": int, "y": int};
129
-
130
-
let coordinatesFromJs: {.. "x": int, "y": int} => coordinates;
131
-
```
132
-
133
-
**Note**:
134
-
135
-
-`coordinatesFromJs` uses an open object type that accepts more fields, just to be more permissive.
136
-
- The converters are shallow. They don't recursively drill into the fields and convert them. This preserves the speed and simplicity of output while satisfying 80% of use-cases.
137
-
138
-
### Usage
139
-
140
-
This exports a `jsCoordinates` JS object (not a record!) for JS files to use:
141
-
142
-
```res
143
-
let jsCoordinates = coordinatesToJs({x: 1, y: 2});
144
-
```
145
-
146
-
This binds to a `jsCoordinates` record (not a JS object!) that exists on the JS side, presumably created by JS calling the function `coordinatesFromJs`:
The above generated functions use JS object types. You can also hide this implementation detail by making the object type **abstract** by using the `newType` option with `@deriving(jsConverter)`:
156
-
157
-
```res
158
-
@deriving({jsConverter: newType})
159
-
type coordinates = {
160
-
x: int,
161
-
y: int,
162
-
}
163
-
```
164
-
165
-
Generates 2 functions of the following types:
166
-
167
-
```resi
168
-
let coordinatesToJs: coordinates => abs_coordinates;
169
-
170
-
let coordinatesFromJs: abs_coordinates => coordinates;
171
-
```
172
-
173
-
#### Usage
174
-
175
-
Using `newType`, you've now prevented consumers from inadvertently doing the following:
176
-
177
-
```res
178
-
let myCoordinates = {
179
-
x: 10,
180
-
y: 20
181
-
};
182
-
let jsCoords = coordinatesToJs(myCoordinates);
183
-
184
-
let x = jsCoords["x"]; /* disallowed! Don't access the object's internal details */
185
-
```
186
-
187
-
Same generated output. Isn't it great that types prevent invalid accesses you'd otherwise have to encode at runtime?
188
-
189
115
## Generate Converters for JS Integer Enums and Variants
190
116
191
117
Use `@deriving(jsConverter)` on a variant type to create converter functions that allow back and forth conversion between JS integer enum and ReScript variant values.
@@ -272,28 +198,6 @@ let kiwi = fruitFromJs(jsKiwi)
> **Note**: Since ReScript v8.2, polymorphic variants are already compiled to strings, so this feature is getting deprecated at some point. It's currently still useful for aliasing JS output with `@as`.
278
-
279
-
Similarly as with [generating int converters](#generate-converters-between-js-integer-enums-and-variants), use `@deriving(jsConverter)` on a polymorphic variant type to create converter functions for JS string and ReScript poly variant values.
280
-
281
-
### Usage
282
-
283
-
```res
284
-
@deriving(jsConverter)
285
-
type fruit = [
286
-
| #Apple
287
-
| @as("miniCoconut") #Kiwi
288
-
| #Watermelon
289
-
]
290
-
291
-
let appleString = fruitToJs(#Apple); /* "Apple" */
292
-
let kiwiString = fruitToJs(#Kiwi); /* "miniCoconut" */
293
-
```
294
-
295
-
As in similar use-cases before, you can also use `@deriving({jsConverter: newType})` to generate abstract types instead.
296
-
297
201
##ConvertRecordTypetoAbstractRecord
298
202
299
203
> **Note**: For ReScript >= v7, we recommend using [plain records to compile to JS objects](bind-to-js-object#bind-to-record-like-js-objects).
0 commit comments