While working with a React + TypeScript project, you may suddenly see this error:
TS7016: Could not find a declaration file for module 'react-router-dom'
or:
implicitly has an 'any' type
This is a very common error when using TypeScript in React applications.
In this article, we’ll clearly understand:
- What this error means
- Why it happens
- How to fix it
- What are declaration files
- Why TypeScript needs them
- Best practices
The Exact Error
Your error looks like this:
TS7016: Could not find a declaration file for module 'react-router-dom'.'/node_modules/react-router-dom/index.js'implicitly has an 'any' type.
And this line causes the error:
import { NavLink } from 'react-router-dom';
What Does This Error Mean?
TypeScript is saying:
“I found the
react-router-dompackage, but I cannot find its type definitions.”
In simple words:
👉 TypeScript understands JavaScript code
❌ But it does NOT know the types of the package.
Why TypeScript Needs Types
TypeScript works using:
- types
- interfaces
- definitions
Example:
let name: string = "Raju";
TypeScript knows:
namemust be string
Similarly, TypeScript wants type information for external libraries too.
What Are Declaration Files?
Declaration files are:
.d.ts
files.
These files tell TypeScript:
- what functions exist
- what props exist
- what return types exist
Example
Without types:
function add(a, b) { return a + b;}
TypeScript does not know:
- what
ais - what
bis
With Types
function add(a: number, b: number): number { return a + b;}
Now TypeScript understands everything clearly.
Why This Error Happens
Usually because:
@types/react-router-dompackage is missing- TypeScript cannot find declaration files
- Package version mismatch
- Incomplete installation
Most Common Fix
Install Type Definitions.
Run:
npm install --save-dev @types/react-router-dom
or
yarn add -D @types/react-router-dom
What This Package Does
This package provides:
type definitions
for:
react-router-dom
Now TypeScript can understand:
NavLinkRouteBrowserRouteruseNavigate- etc.
After Installation
Restart your React server:
npm start
or
npm run dev
Important Version Issue
This is VERY important.
⚠️ React Router v6+
If you are using:
react-router-dom v6 or later
then many times:
👉 type definitions are already included.
You may NOT need:
@types/react-router-dom
Then Why Error Happens?
Usually because:
- corrupted node_modules
- old TypeScript version
- package mismatch
- partial installation
Recommended Fix for React Router v6+
Delete:
node_modulespackage-lock.json
Then reinstall:
npm install
Check Installed Version
Run:
npm list react-router-dom
If Using React Router v5
Then install:
npm install --save-dev @types/react-router-dom
because v5 needs separate type definitions.
How TypeScript Reads Packages
When you import:
import { NavLink } from 'react-router-dom';
TypeScript searches for:
1. package2. type definitions3. .d.ts files
If type definitions are missing:
❌ TS7016 error occurs.
Temporary Quick Fix (Not Recommended)
You may see suggestions like:
declare module 'react-router-dom';
inside a .d.ts file.
This removes the error temporarily.
BUT:
❌ You lose type safety.
Why You Should Avoid This
Because TypeScript will treat everything as:
any
which defeats the purpose of TypeScript.
Best Solution
Always use proper type definitions.
❌ Before Fix
import { NavLink } from 'react-router-dom';
Error:
Could not find declaration file
✅ After Fix
npm install --save-dev @types/react-router-dom
Now error disappears.
Another Possible Issue
Sometimes developers accidentally install:
react-router
instead of:
react-router-dom
Make sure correct package exists.
Verify in package.json
"react-router-dom": "^6.x.x"
Recommended TypeScript Setup
Install:
npm install typescript @types/react @types/react-dom
Important Learning
JavaScript libraries work WITHOUT types.
But TypeScript applications need:
- declarations
- interfaces
- type definitions
to provide:
- autocomplete
- validation
- error checking
- IntelliSense
Final Summary
✔ TS7016 means TypeScript cannot find type definitions
✔ Usually happens with missing @types/... packages
✔ Install correct type definitions
✔ Restart development server after installation
✔ React Router v6+ usually includes types already
✔ Version mismatch can also cause this error
Quick Fix Checklist
Before debugging deeply, check:
✅ react-router-dom installed
✅ correct version installed
✅ @types/react-router-dom installed (for v5)
✅ restart server
✅ delete node_modules and reinstall if needed
🚀 Call to Action
💡 Found this helpful? Subscribe for simple React and TypeScript debugging guides with real-world examples. Happy Coding!