question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could not find a declaration file for module

See original GitHub issue

Bug report

I am importing a specific file from a library on npm, e.g. import file1 from 'someLib/src/file1'. To get its types to work (from @types/someLib), I have to basically reassign its types to that specific file by including a separate someLib.d.ts declaration file in my project that looks like this:

declare module 'someLib/src/file1' {
  import someLib from 'someLib';

  export default someLib;
}

Both tsc and my IDE integration are okay with this and pick up the types, but dts-bundle-generator fails to see it somehow and throws the following error:

src/consumer.ts(1,19): error TS7016: Could not find a declaration file for module 'someLib/src/file1'. 'node_modules/someLib/src/file1.js' implicitly has an 'any' type.

I can get it to pass if I set "strict": false in tsconfig.json, but I don’t want to do that. As a workaround, I can prefix the import with a comment to disable type-checking (// @ts-ignore before import someLib from 'someLib/src/file1) or create a separate tsconfig.json just for dts-bundle-generator with strict disabled, but I thought I should bring it up here to see if it’s a bug or if there’s some configuration I’ve got wrong.

This might be related to #50 but when I tried a triple-slash referenced as proposed there, it included the contents of someLib.d.ts in the generated .d.ts which threw several errors when it attempted to validate the generated file (and I’ve been told the community is moving from triple-slash references to modules anyway?). I don’t understand enough about the declare module and import interop/compatibility yet to fully grasp what’s possible and what isn’t, but I don’t think I can import my someLib.d.ts manually. Also, the usage of someLib is internal only so the types do not need to appear in the output of dts-bundle-generator.

Additional context

CLI command and options:

dts-bundle-generator --umd-module-name myLib --out-file types/myLib.d.ts src/myLib.ts

P.S. Thanks for your work, this project is great. ❤️

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
wKovacs64commented, Jan 6, 2019

Great, no bug after all! Thanks again.

1reaction
wKovacs64commented, Jan 6, 2019

Interesting, nice find! I see also including jssha-src in the types array in tsconfig.json works as an alternative to the triple-slash reference, which I think I prefer.

"typeRoots": ["node_modules/@types", "./src/@types"],
"types": ["node", "jsdom", "jest", "jssha-src"]

Additionally, it seems I can nest the custom module declaration in a structure that mirrors the actual types and refer to it as such (replacing jssha-src with jssha/src):

image

"typeRoots": ["node_modules/@types", "./src/@types"],
"types": ["node", "jsdom", "jest", "jssha/src"]

👍

Thanks @timocov, I think this is a fine workaround for now. I appreciate you digging into this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not find a declaration file for module 'module-name ...
In order for this to work, I had to make declare module '...' the first line of code in the module.d.ts file, and...
Read more >
How to fix error TS7016: Could not find a declaration file for ...
Try `npm install @types/XYZ` if it exists or add a new declaration (.d. · declare module 'XYZ';. Lastly, you also need to add...
Read more >
Could not find declaration file for module 'X' Error | bobbyhadz
The error "Could not find a declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve...
Read more >
Fixing the TS7016 Error | Atomist Blog
Could not find declaration file -- How do you get past the TS7016? This article highlights the eight best and worst fixes.
Read more >
could not find a declaration file for module 'rxjs' angular 13
Check out the possible solutions to solve the issue –. Suppose your module files are stored in dist folder then try remove .js...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found