Skip to content

Commit 949fa9b

Browse files
committed
Rescriptify genType introduction
1 parent 3ad216d commit 949fa9b

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# GenType
22

3-
`genType` lets you export [Reason](https://reasonml.github.io/) values and types to use in JavaScript, and import JavaScript values and types into Reason, idiomatically. Converter functions between the two representations are generated based on the type of the value. The converters can be generated in vanilla JavaScript, or in [TypeScript](https://www.typescriptlang.org/) / [Flow](https://flow.org/en/) for a type-safe idiomatic interface.
3+
`genType` lets you export ReScript values and types to use in JavaScript, and import JavaScript values and types into ReScript, idiomatically. Converter functions between the two representations are generated based on the type of the value. The converters can be generated in vanilla JavaScript, or in [TypeScript](https://www.typescriptlang.org/) / [Flow](https://flow.org/en/) for a type-safe idiomatic interface.
44
In particular, conversion of [ReasonReact](https://reasonml.github.io/reason-react/) components both ways is supported, with automatic generation of the wrappers.
55

6-
Here's an article describing how to use `genType` as part of a migration strategy where a tree of components is gradually converted to Reason bottom-up: [Adopting Reason: strategies, dual sources of truth, and why genType is a big deal](https://medium.com/p/c514265b466d).
6+
Here's an article describing how to use `genType` as part of a migration strategy where a tree of components is gradually converted to ReScript bottom-up (old article containing Reason / BuckleScript): [Adopting Reason: strategies, dual sources of truth, and why genType is a big deal](https://medium.com/p/c514265b466d).
77

8-
The implementation of [@genType] performs a type-directed transformation of Reason programs after [bucklescript](https://github.com/BuckleScript/bucklescript) compilation. The transformed programs operate on data types idiomatic to JS. For example, a Reason function operating on a Reason variant `type t = | A(int) | B(string)` (which is represented as custom blocks at runtime) is exported to a JS function operating on the corresponding JS object of type `{ tag: "A"; value: number }
8+
The implementation of @genType performs a type-directed transformation of ReScript programs after [bucklescript](https://github.com/BuckleScript/bucklescript) compilation. The transformed programs operate on data types idiomatic to JS. For example, a ReScript function operating on a ReScript variant `type t = | A(int) | B(string)` (which is represented as custom blocks at runtime) is exported to a JS function operating on the corresponding JS object of type `{ tag: "A"; value: number }
99
| { tag: "B"; value: string }`.
1010

1111
The output of `genType` can be configured by using one of 3 back-ends: `untyped` to generate wrappers in vanilla JS, `typescript` to generate [TypeScript](https://www.typescriptlang.org/), and `flow` to generate JS with [Flow](https://flow.org/en/) type annotations.
@@ -14,41 +14,39 @@ The output of `genType` can be configured by using one of 3 back-ends: `untyped`
1414

1515
Let's assume we are working on a TypeScript (TS) codebase and we want to integrate a single ReasonReact component.
1616

17-
Firstly we want to be able to import the ReasonReact component like any other React component, secondly we also want to preserve all the Reason types in the TS type system (and convert incompatible values if necessary). **That's exactly what `genType` was made for!**
17+
Firstly we want to be able to import the ReasonReact component like any other React component, secondly we also want to preserve all the ReScript types in the TS type system (and convert incompatible values if necessary). **That's exactly what `genType` was made for!**
1818

1919
First we'll set up a ReasonReact component just like this (we will assume that `genType` and `bs-platform` is already configured correctly):
2020

21-
```re
22-
/* src/MyComp.re */
21+
```res
22+
/* src/MyComp.res */
2323
24-
[@genType]
24+
@genType
2525
type color =
2626
| Red
2727
| Blue;
2828
29-
[@genType]
30-
[@react.component]
29+
@genType
30+
@react.component
3131
let make = (~name: string, ~color: color) => {
3232
let colorStr =
3333
switch (color) {
3434
| Red => "red"
3535
| Blue => "blue"
3636
};
3737
38-
<div className={"color-" ++ colorStr}> name->React.string </div>;
38+
<div className={"color-" ++ colorStr}> {React.string(name)} </div>;
3939
};
4040
```
4141

42-
> **Note:** We need to add a `[@genType]` annotation for `type color` as well,
43-
otherwise `genType` cannot create any type conversions for us.
42+
> **Note:** We need to add a `@genType` annotation for `type color` as well, otherwise `genType` cannot create any type conversions for us.
4443
45-
On a successful compile, `genType` will convert `MyComp.re` to a TS file called
46-
`src/MyComp.gen.ts` which will look something like this:
44+
On a successful compile, `genType` will convert `src/MyComp.res` to a TS file called `src/MyComp.gen.ts` which will look something like this:
4745

4846
```ts
4947
// src/MyComp.gen.tsx
5048

51-
/* TypeScript file generated from MyComp.re by genType. */
49+
/* TypeScript file generated from MyComp.res by genType. */
5250
/* eslint-disable import/first */
5351

5452

@@ -72,29 +70,24 @@ export const make: React.ComponentType<{ readonly color: color; readonly name: s
7270
};
7371
```
7472

75-
Note how `genType` automatically maps the `color` variant to TS via a string
76-
union type `color = "Red" | "Blue"`. We can seamlessly use Reason specific
77-
data structures within TS without doing any manual transformations!
73+
Note how `genType` automatically maps the `color` variant to TS via a string union type `color = "Red" | "Blue"`. We can seamlessly use ReScript specific data structures within TS without doing any manual transformations!
7874

79-
Now, within our TypeScript application, we can now import and use the React
80-
component in following manner:
75+
Now, within our TypeScript application, we can now import and use the React component in following manner:
8176

8277
```ts
8378
// src/App.ts
8479
import { make as MyComp } from "./MyComp.gen.tsx";
8580

8681
const App = () => {
87-
<div>
82+
return (<div>
8883
<h1> My Component </h1>
89-
<MyComp color="Blue" name="Reason & TypeScript" />
90-
</div>
84+
<MyComp color="Blue" name="ReScript & TypeScript" />
85+
</div>);
9186
};
9287
```
9388
94-
We are only scratching the surface on what `genType` can actually do. For more
95-
information, head to the [Getting Started](getting-started) section, or find relevant
96-
topics from the sidebar !
89+
We are only scratching the surface on what `genType` can actually do. For more information, head to the [Getting Started](getting-started) section, or find relevant topics from the sidebar !
9790
9891
## Development
9992
100-
Check out the [`genType` Github repository](https://github.com/reason-association/genType) for more infos.
93+
For contributions, issues or questions, please refer to the [`genType` Github repository](https://github.com/reason-association/genType) or our [forum](https://forum.rescript-lang.org).

0 commit comments

Comments
 (0)