Rescript @genType import 问题
May 20, 2024
Rescript 11 之后,@genType
被合并进编译器,无需任何依赖就能使用,当在 Rescript 中 @genType
了使用某些 Rescript built-in 的基本类型时,可能会生成有问题的 import
相关代码,例如:
1 2 3 4 5 6 7 8 9 10
| @genType module LoginResponse = { let status = response => { response ->Js.Json.decodeObject ->Option.flatMap(response => { response->Js_dict.get("status") }) } }
|
status
函数具有 Js.Json.t => option<Js.Json.t>
类型,那么在生成的 TypeScript 文件中,会出现这样的 import:
1
| import type { Json_t as Js_Json_t } from "./Js.gen.tsx"
|
而 Js.gen.tsx
这个文件是不存在的,解决方案是使用 @genType
的 shim
:
- 在
rescript.json
中的 gentypeconfig
中添加 shim
配置:
1 2 3 4 5 6 7 8 9
| ... "gentypeconfig": { ... "shims": { "Js": "Js" }, ... } ...
|
1 2 3 4 5
| export type Json_t = unknown;
export type t = unknown;
export type Exn_t = Error;
|
- 删除原来由
@genType
生成的 TypeScript 文件并重新生成
现在生成的 TypeScript 文件将会从 Js.shim.ts
import 类型:
1
| import type {Json_t as Js_Json_t} from '../../src/model/Js.shim.ts';
|