|
| 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. |
| 10 | + |
| 11 | +GenType is powerful code generation tool that provides support for both [TypeScript](https://www.typescriptlang.org/) and [Flow](https://flow.org/). It has many features and includes support for [ReasonReact](https://reasonml.github.io/reason-react/) components. |
| 12 | + |
| 13 | +### Example |
| 14 | + |
| 15 | +<CodeTab labels={["ReScript", "TypeScript Output", "Flow Output"]}> |
| 16 | + |
| 17 | +```res |
| 18 | +@genType |
| 19 | +export type color = |
| 20 | + | Red |
| 21 | + | Blue |
| 22 | +
|
| 23 | +@genType @react.component |
| 24 | +export make = (~name: string, ~color: color) => { |
| 25 | + let colorStr = switch color { |
| 26 | + | Red => "red" |
| 27 | + | Blue => "blue" |
| 28 | + } |
| 29 | + <div className={"color-" ++ colorStr}> {React.string(name)} </div> |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +```ts |
| 34 | +/* TypeScript file generated from MyComponent.res by genType. */ |
| 35 | +/* eslint-disable import/first */ |
| 36 | + |
| 37 | +import * as React from "react"; |
| 38 | + |
| 39 | +const $$toRE818596289: { [key: string]: any } = { Red: 0, Blue: 1 }; |
| 40 | + |
| 41 | +// tslint:disable-next-line:no-var-requires |
| 42 | +const MyComponentBS = require("./MyComponent.bs"); |
| 43 | + |
| 44 | +// tslint:disable-next-line:interface-over-type-literal |
| 45 | +export type color = "Red" | "Blue"; |
| 46 | + |
| 47 | +// tslint:disable-next-line:interface-over-type-literal |
| 48 | +export type Props = { readonly color: color; readonly name: string }; |
| 49 | + |
| 50 | +export const make: React.ComponentType<{ |
| 51 | + readonly color: color; |
| 52 | + readonly name: string; |
| 53 | +}> = function MyComponent(Arg1: any) { |
| 54 | + const $props = { color: $$toRE818596289[Arg1.color], name: Arg1.name }; |
| 55 | + const result = React.createElement(MyComponentBS.make, $props); |
| 56 | + return result; |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +```js |
| 61 | +/** |
| 62 | + * @flow strict |
| 63 | + * @generated from MyComponent.res |
| 64 | + * @nolint |
| 65 | + */ |
| 66 | +/* eslint-disable */ |
| 67 | +// $FlowExpectedError: Reason checked type sufficiently |
| 68 | +type $any = any; |
| 69 | + |
| 70 | +// $FlowExpectedError: Reason checked type sufficiently |
| 71 | +import * as React from "react"; |
| 72 | + |
| 73 | +const $$toRE818596289 = { Red: 0, Blue: 1 }; |
| 74 | + |
| 75 | +// $FlowExpectedError: Reason checked type sufficiently |
| 76 | +import * as MyComponentBS from "./MyComponent.bs"; |
| 77 | + |
| 78 | +export type color = "Red" | "Blue"; |
| 79 | + |
| 80 | +// Type annotated function components are not checked by Flow, but typeof() works. |
| 81 | +const make$$forTypeof = function (_: {| |
| 82 | + +color: color, |
| 83 | + +name: string, |
| 84 | +|}): React$Node { |
| 85 | + return null; |
| 86 | +}; |
| 87 | + |
| 88 | +export type Props = {| +color: color, +name: string |}; |
| 89 | + |
| 90 | +export const make: typeof make$$forTypeof = function MyComponent(Arg1: $any) { |
| 91 | + const $props = { color: $$toRE818596289[Arg1.color], name: Arg1.name }; |
| 92 | + const result = React.createElement(MyComponentBS.make, $props); |
| 93 | + return result; |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +</CodeTab> |
| 98 | + |
| 99 | +### References |
| 100 | + |
| 101 | +* [GenType](/docs/gentype/latest/introduction) |
0 commit comments