Skip to content

Commit a296baa

Browse files
committed
Remove stale converter docs
Redirect users who are looking for those to older docs
1 parent 119edf3 commit a296baa

File tree

1 file changed

+7
-103
lines changed

1 file changed

+7
-103
lines changed

pages/docs/manual/latest/generate-converters-accessors.mdx

Lines changed: 7 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ canonical: "/docs/manual/latest/generate-converters-accessors"
66

77
# Generate Converters & Helpers
88

9+
**Note**: if you're looking for:
10+
- `@deriving(jsConverter)` for records
11+
- `@deriving({jsConverter: newType})` for records
12+
- `@deriving(jsConverter)` for polymorphic variants
13+
14+
These particular ones are no longer needed. Select a doc version lower than `9.0` in the sidebar to see their old docs.
15+
916
<!-- TODO: genType -->
1017

1118
When using ReScript, you will sometimes come into situations where you want to
@@ -105,87 +112,6 @@ console.log(Belt_Array.map(pets, name).join("&"));
105112

106113
</CodeTab>
107114

108-
## Generate Converters for JS Object and Record
109-
110-
> **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`:
147-
148-
```res
149-
@module("myGame")
150-
external jsCoordinates : coordinates = "jsCoordinates";
151-
```
152-
153-
### More Safety
154-
155-
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-
189115
## Generate Converters for JS Integer Enums and Variants
190116

191117
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)
272198
let error = fruitFromJs(100) /* nope, can't take a random int */
273199
```
274200

275-
## Generate Converters for JS String Enums and Polymorphic Variants
276-
277-
> **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-
297201
## Convert Record Type to Abstract Record
298202

299203
> **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

Comments
 (0)