typescript - Could not find a declaration file for module module-name.

typescript – Could not find a declaration file for module module-name. /path/to/module-name.js implicitly has an any type

typescript – Could not find a declaration file for module module-name. /path/to/module-name.js implicitly has an any type

Here are two other solutions

When a module is not yours – try to install types from @types:

npm install -D @types/module-name

If the above install errors – try changing import statements to require:

// import * as yourModuleName from module-name;
const yourModuleName = require(module-name);

If youre importing a third-party module foo that doesnt provide any typings, either in the library itself, or in the @types/foo package (generated from the DefinitelyTyped repository), then you can make this error go away by declaring the module in a file with a .d.ts extension. TypeScript looks for .d.ts files in the same places that it will look for normal .ts files: as specified under files, include, and exclude in the tsconfig.json.

// foo.d.ts
declare module foo;

Then when you import foo itll just be typed as any.


Alternatively, if you want to roll your own typings you can do that too:

// foo.d.ts
declare module foo {
    export function getRandomNumber(): number
} 

Then this will compile correctly:

import { getRandomNumber } from foo;
const x = getRandomNumber(); // x is inferred as number

You dont have to provide full typings for the module, just enough for the bits that youre actually using (and want proper typings for), so its particularly easy to do if youre using a fairly small amount of API.


On the other hand, if you dont care about the typings of external libraries and want all libraries without typings to be imported as any, you can add this to a file with a .d.ts extension:

declare module *;

The benefit (and downside) of this is that you can import absolutely anything and TS will compile.

typescript – Could not find a declaration file for module module-name. /path/to/module-name.js implicitly has an any type

If you need a quick fix, simply add this before the line of your import:

// @ts-ignore

Related posts on typescript  :

Leave a Reply

Your email address will not be published.