Skip to content

Commit 3c0845b

Browse files
authored
Merge pull request rescript-lang#144 from reason-association/update-gentype-docs
Update gentype docs
2 parents 74440d1 + b6560da commit 3c0845b

File tree

6 files changed

+207
-275
lines changed

6 files changed

+207
-275
lines changed

common/App.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/App.res

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
let bash = require('highlight.js/lib/languages/bash');
1919
let json = require('highlight.js/lib/languages/json');
2020
let html = require('highlight.js/lib/languages/xml');
21-
let ts = require('highlight.js/lib/languages/typescript');
2221
let text = require('highlight.js/lib/languages/plaintext');
2322
let diff = require('highlight.js/lib/languages/diff');
2423
2524
hljs.registerLanguage('reason', reason);
2625
hljs.registerLanguage('res', res);
2726
hljs.registerLanguage('javascript', js);
28-
hljs.registerLanguage('ts', ts);
27+
hljs.registerLanguage('ts', js);
2928
hljs.registerLanguage('ocaml', ocaml);
3029
hljs.registerLanguage('sh', bash);
3130
hljs.registerLanguage('json', json);

pages/docs/gentype/latest/getting-started.mdx

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88

99
**Requirements:**
1010

11-
`bs-platform` 7.0.2 or higher: use `genType` 3.8.0 or higher.
11+
`bs-platform` 8.3.0 or higher: use `genType` 3.36.0 or higher.
1212

13-
`bs-platform` 7.0.0 or higher: use `genType` 3.2.0 or higher.
13+
`bs-platform` 8.2.0 or higher: use `genType` 3.31.0 or higher.
1414

15-
`bs-platform` 6.2.0 or higher: use `genType` 3.0.0 or higher.
15+
`bs-platform` 8.1.1 or higher: use `genType` 3.27.0 or higher.
1616

17-
`bs-platform` 5.2.0 or higher: use `genType` 2.40.0 or higher.
17+
`bs-platform` 8.0.0 or higher: use `genType` 3.26.0 or higher.
1818

19-
`bs-platform` 5.0.x and 5.1.x: use `genType` 2.17.0 or higher.
20-
21-
For earlier versions, see the older [README](https://github.com/cristianoc/genType/blob/v2.16.0/README.md).
19+
For earlier versions, check the [genType README requirements section](https://github.com/reason-association/genType#requirements).
2220

2321
</Warn>
2422

@@ -44,21 +42,17 @@ Add a `gentypeconfig` section to your `bsconfig.json` (See [Configuration](#conf
4442
}
4543
```
4644

47-
For running `gentype` with BuckleScript via `npm` workflow, add following script in your `package.json`:
45+
For running `gentype` with ReScript via `npm` workflow, add following script in your `package.json`:
4846

4947
```
5048
scripts: {
51-
"bs:build": "bsb -make-world",
52-
"bs:clean": "bsb -clean-world"
49+
"build": "bsb -make-world",
50+
"clean": "bsb -clean-world"
5351
}
5452
```
5553

5654
> **Note:** `bsb` will automatically
5755
> detect your installed `genType` version.
58-
>
59-
> With genType < 2.17.0 or bucklescript < 5.0.0, one has to set
60-
> the environment variable `BS_CMT_POST_PROCESS_CMD`. See the older
61-
> [README](https://github.com/cristianoc/genType/blob/v2.16.0/README.md).
6256
6357
## Configuration
6458

@@ -84,65 +78,51 @@ structure:
8478
- `"untyped"`: Generate `*.gen.js` files in vanilla JavaScript.
8579

8680
- **shims**
87-
- e.g. `Array<string>` with format: `"ReasonModule=JavaScriptModule"`
88-
- Required to export certain basic Reason data types to JS when one cannot modify the sources to add annotations (e.g. exporting Reason lists)
81+
- e.g. `Array<string>` with format: `"RescriptModule=JavaScriptModule"`
82+
- Required to export certain basic ReScript data types to JS when one cannot modify the sources to add annotations (e.g. exporting ReScript lists)
8983

9084
## Adding Shims (TS & Flow)
9185

92-
Configure your shim files in your `"gentypeconfig"` in
93-
[`bsconfig.json`](https://github.com/cristianoc/genType/blob/master/examples/typescript-react-example/bsconfig.json), and add
94-
relevant `.shims.js` files in a directory which is visible by bucklescript e.g.
95-
[`src/shims/`](https://github.com/cristianoc/genType/blob/master/examples/typescript-react-example/src/shims). An example shim to
96-
export ReactEvent can be found
97-
[here](https://github.com/cristianoc/genType/blob/master/examples/typescript-react-example/src/shims/ReactEvent.shim.ts).
86+
A shim is a TS / Flow file that provides user-defined definitions for library types.
87+
88+
Configure your shim files within `"gentypeconfig"` in your [`bsconfig.json`](https://github.com/reason-association/genType/blob/master/examples/typescript-react-example/bsconfig.json), and add relevant `.shims.js` files in a directory which is visible by ReScript e.g. [`src/shims/`](https://github.com/reason-association/genType/blob/master/examples/typescript-react-example/src/shims). An example shim to export ReactEvent can be found [here](https://github.com/reason-association/genType/blob/master/examples/typescript-react-example/src/shims/ReactEvent.shim.ts).
9889

9990
## Testing the Whole Setup
10091

101-
Open any relevant `*.re` file and add `[@genType]` annotations to any bindings
102-
/ values / functions to be used from JavaScript. If an annotated value uses a
103-
type, the type must be annotated too. See e.g.
104-
[Hooks.re](https://github.com/cristianoc/genType/blob/master/examples/typescript-react-example/src/Hooks.re).
92+
Open any relevant `*.res` file and add `@genType` annotations to any bindings / values / functions to be used from JavaScript. If an annotated value uses a type, the type must be annotated too. See e.g. [Hooks.res](https://github.com/reason-association/genType/blob/master/examples/typescript-react-example/src/Hooks.res).
10593

106-
Save the file and rebuild the project via `npm run bs:build` or similar. You
107-
should now see a `*.gen.tsx` (for TypeScript, or `*.gen.js` for Flow) file with
108-
the same name (e.g. `MyComponent.re` -> `MyComponent.gen.tsx`).
94+
Save the file and rebuild the project via `npm run bs:build` or similar. You should now see a `*.gen.tsx` (for TypeScript, or `*.gen.js` for Flow) file with the same name (e.g. `MyComponent.res` -> `MyComponent.gen.tsx`).
10995

110-
Any values exported from `MyComponent.re` can then be imported from JS. For
111-
example:
96+
Any values exported from `MyComponent.res` can then be imported from JS. For example:
11297

11398
```js
11499
import MyComponent from "./components/MyComponent.gen";
115100
```
116101

117-
## Limitations
102+
## Examples
118103

119-
- **BuckleScript in-source = true**. Currently only supports bucklescript
120-
projects with [in-source generation](/docs/manual/latest/build-configuration#package-specs) and `.bs.js` file suffix.
104+
We prepared some examples to give you an idea on how to integrate `genType` in your own project. Check out the READMEs of the listed projects.
121105

122-
- **Limited namespace support**. Currently there's limited [namespace](/docs/manual/latest/build-configuration#name-namespace) support, and only `namespace:true` is possible, not e.g. `namespace:"custom"`.
106+
**Please make sure to build genType before trying to build the examples.**
123107

124-
## Examples
108+
- [flow-react-example](https://github.com/reason-association/genType/tree/master/examples/flow-react-example)
109+
- [typescript-react-example](https://github.com/reason-association/genType/tree/master/examples/typescript-react-example)
110+
- [untyped-react-example](https://github.com/reason-association/genType/tree/master/examples/untyped-react-example)
125111

126-
We prepared some examples to give you an idea on how to integrate `genType` in
127-
your own project. Check out the READMEs of the listed projects.
112+
## Experimental features
128113

129-
- [flow-react-example](https://github.com/cristianoc/genType/tree/master/examples/flow-react-example)
130-
- [typescript-react-example](https://github.com/cristianoc/genType/tree/master/examples/typescript-react-example)
131-
- [untyped-react-example](https://github.com/cristianoc/tree/blob/master/examples/untyped-react-example)
114+
These features are for experimentation only. They could be changed/removed any time, and not be considered breaking changes.
132115

133-
## Experimental features
116+
- Export object and record types as interfaces. To activate, add `"exportInterfaces": true` to the configuration. The types are also renamed from `name` to `Iname`.
134117

135-
These features are for experimentation only. They could be changed/removed any
136-
time, and not be considered breaking changes.
118+
- Emit prop types for the untyped back-end. To activate, add `"propTypes": true` and `"language": "untyped"` to the configuration.
137119

138-
- Export object and record types as interfaces. To activate, add
139-
`"exportInterfaces": true` to the configuration. The types are also renamed
140-
from `name` to `Iname`.
120+
## Limitations
141121

142-
- Emit prop types for the untyped back-end. To activate, add `"propTypes":
143-
true` and `"language": "untyped"` to the configuration.
122+
- **in-source = true**. Currently only supports ReScript projects with [in-source generation](/docs/manual/latest/build-configuration#package-specs) and `.bs.js` file suffix.
123+
124+
- **Limited namespace support**. Currently there's limited [namespace](/docs/manual/latest/build-configuration#name-namespace) support, and only `namespace:true` is possible, not e.g. `namespace:"custom"`.
144125

145126
## Changelog
146127

147-
See [Changes.md](https://github.com/cristianoc/genType/Changes.md) for a
148-
complete list of features, fixes, and changes for each release.
128+
See [Changes.md](https://github.com/reason-association/genType/blob/master/Changes.md) for a complete list of features, fixes, and changes for each release.
Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,56 @@
11
# GenType
22

3-
`genType` lets you export [Reason](/docs/manual/latest/introduction) values and
4-
types to use in JavaScript, and import JavaScript values and types into Reason,
5-
idiomatically. Converter functions between the two representations are
6-
generated based on the type of the value. The converters can be generated in
7-
vanilla JavaScript, or in [TypeScript](https://www.typescriptlang.org/) /
8-
[Flow](https://flow.org/en/) for a type-safe idiomatic interface. In
9-
particular, conversion of [ReasonReact](https://reasonml.github.io/reason-react/)
10-
components both ways is supported, with automatic generation of the wrappers.
11-
12-
Here's an article describing how to use `genType` as part of a migration
13-
strategy where a tree of components is gradually converted to Reason bottom-up:
14-
[Adopting Reason: strategies, dual sources of truth, and why genType is a big
15-
deal](https://www.javierchavarri.com/adopting-reason-strategies-dual-sources-of-truth-and-why-gentype-is-a-big-deal/).
16-
17-
The implementation of [@genType] performs a type-directed transformation of
18-
Reason programs after ReScript compilation. The
19-
transformed programs operate on data types idiomatic to JS. For example, a
20-
Reason function operating on a Reason variant `type t = | A(int) | B(string)`
21-
(which is represented as custom blocks at runtime) is exported to a JS function
22-
operating on the corresponding JS object of type `{ tag: "A"; value: number }
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.
6+
In particular, conversion of [ReasonReact](https://reasonml.github.io/reason-react/) components both ways is supported, with automatic generation of the wrappers.
7+
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).
9+
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 }
2313
| { tag: "B"; value: string }`.
2414

25-
The output of `genType` can be configured by using one of 3 back-ends:
26-
`untyped` to generate wrappers in vanilla JS, `typescript` to generate
27-
[TypeScript](https://www.typescriptlang.org/), and `flow` to generate JS with
28-
[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.
2916

30-
## A More Concrete Example
17+
## A Quick Example
3118

32-
Let's assume we are working on a TypeScript (TS) codebase and we want to
33-
integrate a single ReasonReact component.
19+
Let's assume we are working on a TypeScript (TS) codebase and we want to integrate a single ReasonReact component.
3420

35-
Firstly we want to be able to import the ReasonReact component like any other
36-
React component, secondly we also want to preserve all the Reason types in the
37-
TS type system (and convert incompatible values if necessary). **That's what's
38-
`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).
3922

23+
**That's exactly what genType was made for!**
4024

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

44-
```re
45-
/* src/MyComp.re */
27+
```res
28+
/* src/MyComp.res */
4629
47-
[@genType]
30+
@genType
4831
type color =
4932
| Red
5033
| Blue;
5134
52-
[@genType]
53-
[@react.component]
35+
@genType
36+
@react.component
5437
let make = (~name: string, ~color: color) => {
5538
let colorStr =
5639
switch (color) {
5740
| Red => "red"
5841
| Blue => "blue"
5942
};
6043
61-
<div className={"color-" ++ colorStr}> name->React.string </div>;
44+
<div className={"color-" ++ colorStr}> {React.string(name)} </div>;
6245
};
6346
```
6447

65-
> **Note:** We need to add a `[@genType]` annotation for `type color` as well,
66-
otherwise `genType` cannot create any type conversions for us.
67-
68-
On a successful compile, `genType` will convert `MyComp.re` to a TS file called
69-
`src/MyComp.gen.ts` which will look something like this:
48+
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:
7049

7150
```ts
7251
// src/MyComp.gen.tsx
7352

74-
/* TypeScript file generated from MyComp.re by genType. */
53+
/* TypeScript file generated from MyComp.res by genType. */
7554
/* eslint-disable import/first */
7655

7756

@@ -95,27 +74,28 @@ export const make: React.ComponentType<{ readonly color: color; readonly name: s
9574
};
9675
```
9776

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

99-
Note how `genType` automatically maps the `color` variant to TS via a string
100-
union type `color = "Red" | "Blue"`. We can seamlessly use Reason specific
101-
data structures within TS without doing any manual transformations!
79+
Therefore way we can seamlessly use ReScript specific data structures within TS without writing the converter code by hand!
10280

103-
Now, within our TypeScript application, we can now import and use the React
104-
component in following manner:
81+
Within our TypeScript application, we can now import and use the React component in the following manner:
10582

10683
```ts
10784
// src/App.ts
10885
import { make as MyComp } from "./MyComp.gen.tsx";
10986

11087
const App = () => {
111-
<div>
88+
return (<div>
11289
<h1> My Component </h1>
113-
<MyComp color="Blue" name="Reason & TypeScript" />
114-
</div>
90+
<MyComp color="Blue" name="ReScript & TypeScript" />
91+
</div>);
11592
};
11693
```
11794
118-
We are only scratching the surface on what `genType` can actually do. For more
119-
information, head to the [Getting Started](getting-started) section, or find relevant
120-
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.
98+
99+
## Development
121100
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

Comments
 (0)