【问题标题】:TypeScript errors with "Could not find a declaration file" for *indirect* dependency*间接*依赖的TypeScript错误“找不到声明文件”
【发布时间】:2019-03-06 23:44:25
【问题描述】:

我有以下"noImplicitAny": true 项目,其dependencies(不是devDependencies):

  • wr
    • u
      • bluebird

现在,当我尝试编译 wr 时,出现 tsc 错误:

node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts:1:27 - error TS7016: Could not find a declaration file for module 'bluebird'. '/home/ronjouch/work/wr/node_modules/bluebird/js/release/bluebird.js' implicitly has an 'any' type.
  Try `npm install @types/bluebird` if it exists or add a new declaration (.d.ts) file containing `declare module 'bluebird';`

1 import * as Bluebird from 'bluebird';
                            ~~~~~~~~~~

但是:

  • wrbluebird 无关,我确实希望 @types/bluebirdwr 的 devDeps/deps 中。
  • u 在其 devDependencies 中有 @types/bluebird,这是有道理的,因为它们需要构建 u 但无法运行它,所以我不想将它们移动到 dependencies(正常的 deps / devDeps 细微差别)。

我了解 my 项目代码的错误(并修复),但是当它发生在 indirect 依赖项上时我应该怎么做?是否有 TS 机制来解决这种情况? (忽略依赖代码的noImplicitAny?自动获取类型?)

谢谢。

【问题讨论】:

    标签: typescript bluebird typescript-typings definitelytyped


    【解决方案1】:

    如果这个文件node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts 依赖于@types/bluebird 并被wr 加载,构成u 的公共API 的一部分,那么official recommendation 就是将@types/bluebird 放入dependencies u 正是为了避免这个问题。原则上,package.json 可以支持u 可以在@types/bluebird 上声明的另一种依赖关系,这将导致在wr 上运行npm install 时安装@types/bluebird,但在运行npm install --production 时不会安装。但在我看来,许多包将类型声明捆绑在同一个包中,所以如果udependencies 中有bluebird,则将@types/bluebird 添加到dependencies 并不比bluebird 捆绑它的情况差类型声明,人们不应该抱怨它。

    如果node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts 不是u 的公共 API 的一部分,那么你应该弄清楚为什么 wr 正在加载它并破坏链。

    【讨论】:

    • 将类型包放在dependencies 中对我来说总是感觉不对,但我不得不说这个论点赢得了我的支持:“许多包将类型声明捆绑在同一个包中,所以如果@ 987654346@ 在依赖项中有bluebird,将@types/bluebird 添加到dependencies 并不比bluebird 捆绑其类型声明更糟糕。”所以谢谢。当然,最好的解决方案是package.json 中的一种新型依赖关系,它会传播给消费者,但仅适用于开发。
    猜你喜欢
    • 2020-11-28
    • 2019-01-18
    • 2018-12-09
    • 2016-04-09
    • 2017-06-05
    • 2017-02-16
    • 2022-08-09
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多