为包含内部模块的包添加 Typescript 声明

Adding Typescript declaration for package with inner modules

我想将类型添加到完全用 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. 的目录
  4. 我还更改了声明以具有名为 companymodule__company-widgets[= 的 namespace 40=]
  5. 我也很绝望,开始在 node_modules 中放置一个声明

None个不幸找到了声明,所以我一直在想无法将声明中的模块名称与包名称相关联。 任何建议或答案将不胜感激。谢谢!

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