could not find a declaration file for module – React TypeScript Error

Total
0
Shares

React Typescript throws the error, could not find a declaration file for module, due to number of reasons. Check out the possible solutions to solve the issue –

Suppose your module files are stored in dist folder then try remove .js extension from "main" key in package.json

Replace this –

"main": "dist/index.js",

with this –

"main": "dist/index",

Also, you can try adding typings key to the package.json file –

"main": "dist/index",
"typings": "dist/index",

If the module in which you are getting error, is not yours then you may try this command –

npm install -D @types/module-name

If you get error in the above command, you can change the import to require –

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

You may also declare the module in *.d.ts file. This will shut the error down.

Suppose your module is 'superhero'. Create a file superhero.d.ts and add the following in that –

declare module 'superhero';

You may also use your own typings for the parts you are going to use in your project. For example –

declare module 'superhero' {
    export function getHeroName(): string
}

Now you can easily use this as –

import { getHeroName } from 'superhero';
const heroName = getHeroName();

    Tweet this to help others

These solutions will remove the error of “cannot find declaration file for module” in React typescript.