Search code examples
typescriptbluebirdtypescript-typingsdefinitelytyped

TypeScript errors with "Could not find a declaration file" for *indirect* dependency


I have the following "noImplicitAny": true project, with its dependencies (not devDependencies):

  • wr
    • u
      • bluebird

Now, when I try to compile wr, tsc errors with:

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';
                            ~~~~~~~~~~

But:

  • wr has nothing to do with bluebird, I do not want @types/bluebird in wr's devDeps/deps.
  • u has @types/bluebird in its devDependencies and that makes sense because they're required to build u but useless to run it, so I do not want to move them to dependencies (normal deps / devDeps nuance).

I understand the error (and fix) when it comes to my project code, but what am I supposed to do when it happens like here on an indirect dependency ? Is there a TS mechanism to cover that case? (Ignore the noImplicitAny for dependencies code? Automatically acquire typings?)

Thanks.


Solution

  • If this file node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts that depends on @types/bluebird and is getting loaded by wr forms part of the public API of u, then the official recommendation is to put @types/bluebird in the dependencies of u precisely to avoid this issue. In principle, package.json could support another kind of dependency that u could declare on @types/bluebird that would cause @types/bluebird to be installed when you run npm install on wr but not when you run npm install --production. But the way I see it, many packages bundle the type declarations in the same package, so if u has bluebird in dependencies, adding @types/bluebird to dependencies is no worse than if bluebird bundled its type declarations, and people shouldn't complain about it.

    If node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts is not part of the public API of u, then you should figure out why wr is loading it and break the chain.