Skip to content

Update to gentype v4 docs #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions pages/docs/gentype/latest/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add a `gentypeconfig` section to your `bsconfig.json` (See [Configuration](#conf

```
"gentypeconfig": {
"language": "untyped",
"language": "typescript",
"shims": {},
"debug": {
"all": false,
Expand All @@ -35,22 +35,14 @@ Add a `gentypeconfig` section to your `bsconfig.json` (See [Configuration](#conf
}
```

For running `gentype` with ReScript via `npm` workflow, add following script in your `package.json`:

```
scripts: {
"build": "rescript",
"clean": "rescript clean"
}
```
For running `genType` with ReScript via `npm` workflow, no special setup is required in `package.json`.

> **Note:** `rescript` will automatically detect your installed `genType` version.

## Configuration

Every `genType` powered project requires a configuration item `"gentypeconfig"`
at top level in the project's `bsconfig.json`. (The use of a separate file
`gentypeconfig.json` is being deprecated). The configuration has following
at top level in the project's `bsconfig.json`. The configuration has following
structure:

```js
Expand All @@ -70,12 +62,12 @@ structure:
- `"untyped"`: Generate `*.gen.js` files in vanilla JavaScript.

- **shims**
- e.g. `Array<string>` with format: `"RescriptModule=JavaScriptModule"`
- Required to export certain basic ReScript data types to JS when one cannot modify the sources to add annotations (e.g. exporting ReScript lists)
- Required only if one needs to export certain basic ReScript data types to JS when one cannot modify the sources to add annotations (e.g. exporting ReScript lists), and if the types are not first-classed in genType.
- Example: `Array<string>` with format: `"RescriptModule=JavaScriptModule"`

## Adding Shims (TS & Flow)

A shim is a TS / Flow file that provides user-defined definitions for library types.
A shim is a TS / Flow file that provides user-provided definitions for library types.

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

Expand All @@ -97,8 +89,8 @@ We prepared some examples to give you an idea on how to integrate `genType` in y

**Please make sure to build genType before trying to build the examples.**

- [flow-react-example](https://github.com/reason-association/genType/tree/master/examples/flow-react-example)
- [typescript-react-example](https://github.com/reason-association/genType/tree/master/examples/typescript-react-example)
- [flow-react-example](https://github.com/reason-association/genType/tree/master/examples/flow-react-example)
- [untyped-react-example](https://github.com/reason-association/genType/tree/master/examples/untyped-react-example)

## Experimental features
Expand Down
6 changes: 0 additions & 6 deletions pages/docs/gentype/latest/supported-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ For example:
let exampleFunction = (~_type) => "type: " ++ _type
```

## Record Components

ReasonReact record components with props of ReScript types `t1`, `t2`, `t3` are exported as reactjs components with props of the JS types corresponding to `t1`, `t2`, `t3`. The annotation is on the `make` function: `[@genType] let make ...`.

A file can export many components by defining them in sub-modules. The toplevel component is also exported as default.

## Imported Types

It's possible to import an existing TS/Flow type as an opaque type in ReScript. For example,
Expand Down
38 changes: 1 addition & 37 deletions pages/docs/gentype/latest/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,9 @@ Similarly, to import a value with a different JS name, use e.g. `@genType.import

To import nested values, e.g. `Some.Nested.value`, use e.g. `@genType.import(("./MyMath", "Some.Nested.value"))`.

## Export and Import React Components Using Deprecated Record API

**Important:** This section is only relevant for legacy [reason-react](https://github.com/reasonml/reason-react) codebases.

To export a ReasonReact component to JS, and automatically generate a wrapper for it, simply annotate the `make` function:

```res
@genType
let make = (~onClick: callback, _children) => {
...component,
render: _ => <div onClick> {ReasonReact.string("Click me")} </div>,
};
```

**NOTE:** the value `component` must also be defined, above `make` in the same module (also in the case of components defined in nested modules).


To import and wrap a ReactJS component for use by ReasonReact, the type of the `make` function is the only information required:

```res
@genType.import("./MyBanner") /* Module with the JS component to be wrapped. */
/* The make function will be automatically generated from the types below. */
external make:
(~show: bool, ~message: option<message>=?, 'a) =>
ReasonReact.component<
ReasonReact.stateless,
ReasonReact.noRetainedProps,
ReasonReact.actionless,
> =
"make";
```

The type of `make` must have a named argument for each prop in the JS component. Optional props have option type. The `make` function will be generated by `genType`.

This assumes that the JS component was exported with a default export. In case of named export, use e.g. `@genType.import(("./MyBanner", "componentName"))`. To import a nested component, use e.g. `@genType.import(("./MyBanner", "Some.Nested.component"))`.

## Interface (.resi) and Implementation (.res) files

If both `Foo.resi` and `Foo.res` exist, the annotations are taken from `Foo.resi`.
If both `Foo.resi` and `Foo.res` exist, the annotations are taken from `Foo.resi`. The same happens with local modules: if present, the module type gets precedence.

The behaviour can be overridden by adding annotation `@genType.ignoreInterface` at the top of `Foo.resi`. Use case: expose implementation details to JS but not to ReScript.

Expand Down
2 changes: 1 addition & 1 deletion src/layouts/GenTypeDocsLayout.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function GenTypeDocsLayout(Props) {
breadcrumbs: breadcrumbs,
title: "GenType",
metaTitleCategory: "ReScript GenType",
version: "v3",
version: "v4",
components: components,
theme: "Reason",
children: children
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/GenTypeDocsLayout.res
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let make = (~frontmatter=?, ~components=Markdown.default, ~children) => {
}

let title = "GenType"
let version = "v3"
let version = "v4"

<LatestLayout
theme=#Reason
Expand Down