|
| 1 | +--- |
| 2 | +id: "gentype-decorator" |
| 3 | +keywords: ["gentype", "decorator", "typescript", "flow"] |
| 4 | +name: "@genType" |
| 5 | +summary: "This is the `@genType` decorator." |
| 6 | +category: "decorators" |
| 7 | +--- |
| 8 | + |
| 9 | +The `@genType` decorator may be used to export ReScript values and types to JavaScript, and import JavaScript values and types into ReScript. It allows seamless integration of compiled ReScript modules in existing TypeScript, Flow, or plain JavaScript codebases, without loosing type information across different type systems. |
| 10 | + |
| 11 | +[GenType](/docs/gentype/latest/introduction) is a code generation tool for automatically generating [TypeScript](https://www.typescriptlang.org/) / [Flow](https://flow.org/) type definitions, and JS runtime converters for non-shared ReScript values. It also features first-class support for [ReasonReact](https://reasonml.github.io/reason-react/) components. |
| 12 | + |
| 13 | +**Note:** This decorator requires the `gentype` npm package to be installed and configured correctly. Please refer to genType's [Getting Started](/docs/gentype/latest/getting-started) section for more details. |
| 14 | + |
| 15 | +### Example |
| 16 | + |
| 17 | +<CodeTab labels={["ReScript", "TypeScript Output", "Flow Output"]}> |
| 18 | + |
| 19 | +```res |
| 20 | +@genType |
| 21 | +export type color = |
| 22 | + | Red |
| 23 | + | Blue |
| 24 | +
|
| 25 | +@genType @react.component |
| 26 | +export make = (~name: string, ~color: color) => { |
| 27 | + let colorStr = switch color { |
| 28 | + | Red => "red" |
| 29 | + | Blue => "blue" |
| 30 | + } |
| 31 | + <div className={"color-" ++ colorStr}> {React.string(name)} </div> |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +```ts |
| 36 | +/* TypeScript file generated from MyComponent.res by genType. */ |
| 37 | +/* eslint-disable import/first */ |
| 38 | + |
| 39 | +import * as React from "react"; |
| 40 | + |
| 41 | +const $$toRE818596289: { [key: string]: any } = { Red: 0, Blue: 1 }; |
| 42 | + |
| 43 | +// tslint:disable-next-line:no-var-requires |
| 44 | +const MyComponentBS = require("./MyComponent.bs"); |
| 45 | + |
| 46 | +// tslint:disable-next-line:interface-over-type-literal |
| 47 | +export type color = "Red" | "Blue"; |
| 48 | + |
| 49 | +// tslint:disable-next-line:interface-over-type-literal |
| 50 | +export type Props = { readonly color: color; readonly name: string }; |
| 51 | + |
| 52 | +export const make: React.ComponentType<{ |
| 53 | + readonly color: color; |
| 54 | + readonly name: string; |
| 55 | +}> = function MyComponent(Arg1: any) { |
| 56 | + const $props = { color: $$toRE818596289[Arg1.color], name: Arg1.name }; |
| 57 | + const result = React.createElement(MyComponentBS.make, $props); |
| 58 | + return result; |
| 59 | +}; |
| 60 | +``` |
| 61 | + |
| 62 | +```js |
| 63 | +/** |
| 64 | + * @flow strict |
| 65 | + * @generated from MyComponent.res |
| 66 | + * @nolint |
| 67 | + */ |
| 68 | +/* eslint-disable */ |
| 69 | +// $FlowExpectedError: Reason checked type sufficiently |
| 70 | +type $any = any; |
| 71 | + |
| 72 | +// $FlowExpectedError: Reason checked type sufficiently |
| 73 | +import * as React from "react"; |
| 74 | + |
| 75 | +const $$toRE818596289 = { Red: 0, Blue: 1 }; |
| 76 | + |
| 77 | +// $FlowExpectedError: Reason checked type sufficiently |
| 78 | +import * as MyComponentBS from "./MyComponent.bs"; |
| 79 | + |
| 80 | +export type color = "Red" | "Blue"; |
| 81 | + |
| 82 | +// Type annotated function components are not checked by Flow, but typeof() works. |
| 83 | +const make$$forTypeof = function (_: {| |
| 84 | + +color: color, |
| 85 | + +name: string, |
| 86 | +|}): React$Node { |
| 87 | + return null; |
| 88 | +}; |
| 89 | + |
| 90 | +export type Props = {| +color: color, +name: string |}; |
| 91 | + |
| 92 | +export const make: typeof make$$forTypeof = function MyComponent(Arg1: $any) { |
| 93 | + const $props = { color: $$toRE818596289[Arg1.color], name: Arg1.name }; |
| 94 | + const result = React.createElement(MyComponentBS.make, $props); |
| 95 | + return result; |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +</CodeTab> |
| 100 | + |
| 101 | +### References |
| 102 | + |
| 103 | +* [GenType](/docs/gentype/latest/introduction) |
0 commit comments