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/introduction.mdx
+20-10Lines changed: 20 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,28 @@
1
1
# GenType
2
2
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.
3
+
`genType` is a code generation tool that lets you export ReScript values and types to use in JavaScript, and import JavaScript values and types into ReScript.
4
+
5
+
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.
4
6
In particular, conversion of [ReasonReact](https://reasonml.github.io/reason-react/) components both ways is supported, with automatic generation of the wrappers.
5
7
6
8
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).
7
9
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}
10
+
The implementation of genType performs a type-directed transformation of ReScript programs after ReScript source code compilation. The transformed programs operate on data types idiomatic to JS.
11
+
12
+
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}
9
13
| {tag: "B"; value: string}`.
10
14
11
-
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.
15
+
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.
12
16
13
-
## A More Concrete Example
17
+
## A Quick Example
14
18
15
19
Let's assume we are working on a TypeScript (TS) codebase and we want to integrate a single ReasonReact component.
16
20
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!**
21
+
We want to be able to import the ReasonReact component like any other React component in our existing TS code, but we also want to preserve all the ReScript types in the TS type system (and convert incompatible values if necessary).
22
+
23
+
**That's exactly what genType was made for!**
18
24
19
-
First we'll set up a ReasonReact component just like this (we will assume that `genType` and `bs-platform` is already configured correctly):
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!
77
+
genType automatically maps the `color` variant to TS via a string union type `color = "Red" | "Blue"`, and also provides all the converters to convert between the ReScript & TS representation as well.
72
78
73
-
Now, within our TypeScript application, we can now import and use the React component in following manner:
79
+
Therefore way we can seamlessly use ReScript specific data structures within TS without writing the converter code by hand!
80
+
81
+
Within our TypeScript application, we can now import and use the React component in the following manner:
74
82
75
83
```ts
76
84
// src/App.ts
@@ -84,8 +92,10 @@ const App = () => {
84
92
};
85
93
```
86
94
87
-
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 !
95
+
That's it for our quick example.
96
+
97
+
For detailed information, head to the [Getting Started](getting-started) or [Usage](usage) section.
88
98
89
99
## Development
90
100
91
-
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).
101
+
For contributions, issues or questions, please refer to the [Github repository](https://github.com/reason-association/genType) or our [ReScript forum](https://forum.rescript-lang.org).
0 commit comments