Error "Could not find a declaration file for module" in TypeScript — how to fix?
Hello! I am a beginner TypeScript developer and encountered a problem when importing a third-party module. I have a simple Express.js project written in TypeScript. I installed the `lodash` package via npm, but when I try to import it in my `index.ts` file, the compiler throws an error.
Here is my code:
```typescript
import _ from 'lodash';
const arr = [1, 2, 3, 4, 5];
const shuffled = _.shuffle(arr);
console.log(shuffled);
```
When running `tsc`, I get:
```
Could not find a declaration file for module 'lodash'. '/path/to/node_modules/lodash/lodash.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/lodash` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash';`
```
I understand that TypeScript requires declaration files (`.d.ts`) for third-party modules, but I don't fully understand how this works. Why do some modules work without additional actions, while others do not? And what does "implicitly has an 'any' type" mean?
No answers yet.
Log in to leave an answer
Blogs
Working with the File System in PHP: A Complete Guide for Beginners
Containerizing Kotlin Applications with Docker: A Complete Guide
CLI Utilities in Swift: Building Command Line Tools from Scratch
String Concatenation in Go: How Not to Kill Performance