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
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).
98
89
99
90
## Testing the Whole Setup
100
91
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
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).
105
93
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`).
109
95
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:
-**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.
121
105
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.**
`"exportInterfaces": true` to the configuration. The types are also renamed
140
-
from `name` to `Iname`.
120
+
## Limitations
141
121
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"`.
144
125
145
126
## Changelog
146
127
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.
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}
23
13
| {tag: "B"; value: string}`.
24
14
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.
29
16
30
-
## A More Concrete Example
17
+
## A Quick Example
31
18
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.
34
20
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).
39
22
23
+
**That's exactly what genType was made for!**
40
24
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):
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.
98
78
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!
102
80
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:
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
121
100
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