【问题标题】:Using custom typeRoots and still exporting those types along with entrypoint使用自定义 typeRoots 并仍然将这些类型与入口点一起导出
【发布时间】: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.tssrc/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


    【解决方案1】:

    现在,根据Best practice for exporting types from declaration file to external project 的建议,我将停止使用typeRoots.d.ts

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2018-03-07
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多