The error "could not find declaration file for module 'react'" occurs when TypeScript cannot find the type declaration for a react-related module. To solve the error install the types for the module by running the command from the error message, e.g. npm install -D @types/react .
When you have this error, it is usually because you installed a library and you didn't install the types of that library. To fix this error, you need to install @types/library-name using your package manager (npm or yarn).
The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...
I had a similar error but for me it was react-router
. Solved it by installing types for it.
npm install --save @types/react-router
Error:
(6,30): error TS7016: Could not find a declaration file for module 'react-router'. '\node_modules\react-router\index.js' implicitly has an 'any' type.
If you would like to disable it site wide you can instead edit tsconfig.json
and set noImplicitAny
to false
.
For those who wanted to know that how did I overcome this . I did a hack kind of stuff .
Inside my project I created a folder called @types
and added it to tsconfig.json for find all required types from it . So it looks somewhat like this -
"typeRoots": [
"../node_modules/@types",
"../@types"
]
And inside that I created a file called alltypes.d.ts
. To find the unknown types from it . so for me these were the unknown types and I added it over there.
declare module 'react-materialize';
declare module 'react-router';
declare module 'flux';
So now the typescript didn't complain about the types not found anymore . :) win win situation now :)
I've had a same problem with react-redux types. The simplest solution was add to tsconfig.json:
"noImplicitAny": false
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["es6"],
"moduleResolution": "node",
"noEmit": true,
"strict": true,
"target": "esnext",
"noImplicitAny": false,
},
"exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
}
If there is no @types/<package>
for the module you are using, you may easily circumvent the issue by adding a // @ts-ignore
comment above i.e.
// @ts-ignore
import { Navbar, NavItem } from 'react-materialize';
Alternatively you may create the missing @types/<package>
following:
declaration files publishing
DefinitelyTyped how can i contribute
Make sure to stop your react local server and start it again after doing the following:
1- Create .d.ts
file manually, you just need to do the following:
2 - enter src folder
3 - create global.d.ts
file
4 - declare modules in it like:
declare module 'module-name';
I answered it here before and it was nice
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With