【发布时间】:2021-02-19 00:18:48
【问题描述】:
在我的项目中,我使用汇总为我的入口点 src/index.ts 捆绑相应的 index.d.ts(和 index.js)。
在内部,我有一个 src/types 文件夹,其中包含多个 .d.ts 文件。这些类型在我的项目中的全局范围内可用,因为src/types 在我的typeRoots 中,在tsconfig.json 中。
但是,由于我的项目的 入口点 是 index.ts,因此这些 全局 类型不会导出给我的 npm 模块的用户。我必须在我的index.ts 文件中重新声明并重新导出它们,以便它们可以在我的项目之外导入。有没有办法通过我的入口点index.ts 在src/types 中包含(///reference)我的类型定义,同时在我的项目中保留方便的全局访问权限?
例子:
// src/types/foo.d.ts
interface Foo {
bar: string;
}
// many many more types....
// src/index.ts
// suppose it is very inconvenient for me to
// instead use myProject.Foo or some other prefix:
export const HelloWorld = (input: Foo) => console.log("hello world", input.bar);
// tsconfig.json
{
...
"compilerOptions": {
...,
"declaration": true,
"typeRoots": ["src/types", "node_modules/@types"]
}
...
}
在我的项目的假设消费中:
import {HelloWorld, Foo} from "myModuleAbove";
const input: Foo = {bar: "hi"};
HelloWorld(input);
方法和https://stackoverflow.com/a/47939706/10833799类似吗?
谢谢!
【问题讨论】:
标签: typescript npm rollupjs