【问题标题】:Adding Typescript declaration for package with inner modules为带有内部模块的包添加 Typescript 声明
【发布时间】:2018-09-28 05:57:54
【问题描述】:

我想将类型添加到纯粹用 Javascript 编写的组件库中,我目前无法直接添加到该组件库中。我已经按照adding declarations to third party packages 的这个示例进行操作,这几乎让我到达了那里。我目前遇到的问题是包结构。

该包位于node_modules@company/company-widgets,我不确定如何处理内部模块,因为tsc 无法找到并关联我添加的声明文件。编译器的建议是 Try `npm install @types/company__company-widgets` if it exists or add a new declaration (.d.ts) file containing `declare module 'company__company-widgets';`

我尝试了以下方法:

  1. 使用编译器消息中指定的模块声明修改 tsconfig.json"typeRoots": ["@types", "./src/@types"] and adding anindex.d.ts` 文件
  2. 随着上述tsconfig.json 的更改,在src/@types 下添加目录以匹配node_modules 的目录
  3. 我还将声明更改为具有名为 companymodule__company-widgetsnamespace
  4. 我也很绝望,并开始在node_modules 中添加声明来搞乱

不幸的是,这些都没有找到声明,所以我一直认为它无法将声明中的模块名称与包名称相关联。 任何建议或答案将不胜感激。谢谢!

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    编译器的建议是错误的:模块声明应该是declare module '@company/company-widgets';,而不是declare module 'company__company-widgets';。我 fixed this recently 并且修复应该在 TypeScript 3.1 中。

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      相关资源
      最近更新 更多