Dav*_*gel 5 node.js npm typescript purescript
TL; DR我想为已编译的PureScript模块创建TypeScript类型,并将其分布在我的npm包中。我很乐于手动维护这些类型,只是无法弄清楚需要在tsconfig.json(上下)和package.json中放入什么。
我有一个项目,其中的核心功能是使用TypeScript CLI在PureScript中实现的,所有这些最终都通过npm以JavaScript的形式分发。我用一个简单的布局创建了一个类似的项目:
.
??? cli # TypeScript CLI project
? ??? main.ts
? ??? tsconfig.json
?
??? output # Compiled PureScript modules
? ??? People
? ??? externs.json
? ??? index.js
?
??? src # PureScript source
? ??? People.purs
?
??? types # TypeScript types for PureScript modules
??? People.d.ts
Run Code Online (Sandbox Code Playgroud)
在src/People.purs我定义的核心功能:
module People where
type Person = { name :: String }
david :: Person
david = { name: "David "}
Run Code Online (Sandbox Code Playgroud)
在“ types/People.d.ts我为PureScript模块定义TypeScript类型”中,以便可以从TypeScript中安全地使用它们:
.
??? cli # TypeScript CLI project
? ??? main.ts
? ??? tsconfig.json
?
??? output # Compiled PureScript modules
? ??? People
? ??? externs.json
? ??? index.js
?
??? src # PureScript source
? ??? People.purs
?
??? types # TypeScript types for PureScript modules
??? People.d.ts
Run Code Online (Sandbox Code Playgroud)
最后,在中cli/main.ts,我要导入已定义的TypeScript类型的已编译PureScript模块:
module People where
type Person = { name :: String }
david :: Person
david = { name: "David "}
Run Code Online (Sandbox Code Playgroud)
我可以构建它,但是当我尝试运行最终的JavaScript(node cli/main.js)时,由于它们不使用绝对路径require而tsc导致的调用失败- 在这种情况下require("People")应该require("./People")如此。使importTypeScript中的语句成为相对的将取消关联。
我很难达到以下目标:
require对PureScript模块的调用在运行时解决。如果您有从TypeScript要求PureScript模块并通过npm分发所有这些的经验,我将不胜感激!
声明命名空间而不是模块使一切正常。例如,在types/People.d.ts:
declare namespace People {
export interface Person {
name: string;
}
export const david: Person;
}
export = People;
export as namespace People;
Run Code Online (Sandbox Code Playgroud)
在编译 PureScript 模块之后和编译 TypeScript 之前,我cp types/People.d.ts output/People/index.d.ts. 这使得 TypeScript 代码对相同的绝对导入感到满意(例如import * as People from "People";),并且 TypeScript 库也无需额外配置即可看到这些类型。
但仍然存在一些问题:
../在构建后步骤中添加前缀);不过,我的图书馆的消费者不必这样做,因为它会通过npm并且神奇地使其发挥作用。Data.Maybe类似的模块由Data_Maybe.| 归档时间: |
|
| 查看次数: |
394 次 |
| 最近记录: |