【问题标题】:Export single .d.ts from several typescript files + entrypoint从多个打字稿文件+入口点导出单个 .d.ts
【发布时间】:2017-02-04 23:14:46
【问题描述】:

我有几个 .ts 文件 + 1 个入口点,如下所示:

  • classA.ts
  • classB.ts
  • entrypoint.ts

entrypoint.ts 包含类似于:

export * from './classA';
export * from './classB';

我想要一个 .d.ts 描述所有入口点导出的内容,以便包含 ClassAClassB 定义文件。

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    您不能自动生成单个 d.ts 文件。以下是可行的(假设您正在构建一个库/可重用模块):

    1. 通过在tsconfig.json 中指定"declaration": true,让编译器为您的类自动生成声明

    2. 编译器还将生成一个entrypoint.d.ts 文件(重新导出classA 和classB 的d.ts

    3. package.jsontypings 条目指向entrypoint.d.ts,例如"typings": "./dist/entrypoint.d.ts"

    假设您的库名为 library,您现在可以将其安装在项目的 node_modules 中并通过标准导入使用它:

    import {classA, classB} from 'library'
    

    将为这些类导入生成的d.ts.

    【讨论】:

    • 第 2 步对我来说没有发生。 index.d.ts 文件(它导出项目中的所有其他 *.ts 文件,只有一行“export {};”
    【解决方案2】:

    对于任何发现这篇文章的人。您可以尝试 npm-dts 实用程序。这应该可以解决问题。

    【讨论】:

    • 成功了-谢谢
    【解决方案3】:

    这个对我有用,而且非常简单:https://github.com/TypeStrong/dts-bundle

    我没有运气尝试了这些:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 2019-04-10
      • 2021-09-13
      • 2019-01-02
      • 2018-03-04
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多