Skip to content

Commit 1f70447

Browse files
Add example of whole module @genType annotation
1 parent 445d220 commit 1f70447

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pages/docs/manual/latest/typescript-integration.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ import { printColorMessage } from "./Color.gen.tsx";
8585
printColorMessage("Red", "Hello, genType!");
8686
```
8787

88+
## Exporting an entire module
89+
90+
_Since ReScript `11.0.0`_ modules can be annotated with `@genType` as well. In that case, all types and values of the module will be converted to TS types. Example:
91+
92+
<CodeTab labels={["ReScript", "TypeScript Output"]}>
93+
94+
```res example
95+
@genType
96+
module Size = {
97+
type t =
98+
| Small
99+
| Medium
100+
| Large
101+
102+
let getNum = (size: t) =>
103+
switch size {
104+
| Small => 1.
105+
| Medium => 5.
106+
| Large => 10.
107+
}
108+
}
109+
```
110+
111+
```ts
112+
import * as MyCompBS__Es6Import from './MyComp.res';
113+
const MyCompBS: any = MyCompBS__Es6Import;
114+
115+
export type Size_t = "Small" | "Medium" | "Large";
116+
117+
export const Size_getNum: (size:Size_t) => number = MyCompBS.Size.getNum;
118+
119+
export const Size: { getNum: (size:Size_t) => number } = MyCompBS.Size
120+
```
121+
</CodeTab>
122+
123+
88124
## Setup
89125

90126
Add a `gentypeconfig` section to your `rescript.json` (See [Configuration](/docs/manual/latest/build-configuration#gentypeconfig) for details).

0 commit comments

Comments
 (0)