【问题标题】:Generate declaration file programmatically以编程方式生成声明文件
【发布时间】:2021-08-24 03:08:59
【问题描述】:

我知道我可以运行tsc --declaration myfile.ts 来生成声明文件。有没有什么方法可以用 TS 编译器以编程方式做到这一点?

比如

const ts = require('typescript');
const out = ts.declarations('source-string');

【问题讨论】:

  • 我认为没有单行。一般的想法是从像this one这样的示例编译器开始,启用declarationemitDeclarationOnly编译器选项,并传递一个自定义CompilerHost,其getSourceFile方法返回您的源代码,其writeFile方法捕获声明。我没有兴趣将完整的工作样本放在一起,但也许其他人会这样做。

标签: typescript typescript-compiler-api


【解决方案1】:

我认为这个 github 项目可以为您提供解决方案: https://github.com/SitePen/dts-generator

开始:npm install dts-generator

以编程方式生成您的 d.ts 包:

require('dts-generator').default({
    name: 'package-name',
    project: '/path/to/package-directory',
    out: 'package-name.d.ts'
});

有关更多信息,您应该访问上面的链接:)。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2018-01-29
    • 2014-08-10
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多