TypeScript
Completed
-
Section 1: Getting Started
-
Section 2: Basic Types
-
Section 3: Functions and Objects
-
Section 4: Advanced Types
-
Section 5: Object-Oriented TypeScript
-
Section 6: Working with Modules
-
Section 7: Tooling and Practice
-
Section 8: Type-Level Programming
-
Section 9: TypeScript with Backends
-
Section 10: Testing Typed Code
-
Section 11: Data Validation with Types
-
Section 12: Practical Projects
-
Section 13: Compiler Internals
-
Section 14: Configuration Deep Dive
-
Section 15: Enums, Symbols, and Special Types
-
Section 16: Working with Async Code
-
Section 17: TypeScript and the DOM
-
Section 18: Advanced Generics Practice
-
Section 19: Working with Third-Party Types
-
Section 20: Monorepo and Large-Scale Practices
-
Section 21: Common Pitfalls and Best Practices
-
Section 22: Interview Practice
-
Section 23: Handbook: Narrowing In Depth
-
Section 24: Handbook: Object Types In Depth
-
Section 25: Handbook: Classes In Depth
-
Section 26: Handbook: Modules In Depth
-
Section 27: Handbook: Declaration Files In Depth
-
Section 28: JSX and Namespaces
-
Section 29: Compiler Configuration Reference
-
Section 30: More Practice Exercises
-
Section 31: Handbook: Everyday Types Deep Dive
-
Section 32: Utility Types Full Reference
-
Section 33: Decorators Reference
-
Section 34: Mixins and Advanced OOP Patterns
-
Section 35: Iterators and Generators Typing
-
Section 36: More Type-Level Programming Practice
-
Section 37: TypeScript Ecosystem Tools
-
Section 38: TypeScript with Testing Frameworks
-
Section 39: TypeScript for Library Authors
-
Section 40: More Interview Practice
-
Section 41: Handbook: Functions In Depth
-
Section 42: Handbook: Type Manipulation Deep Dive
-
Section 43: More Real-World Patterns
-
Section 44: TypeScript Release Notes Highlights
-
Section 45: Final Practice and Review
162: DefinitelyTyped and @types Packages
Why is TypeScript complaining that it "could not find a declaration file" for a library I just installed?
You've probably seen this error a dozen times by now: Could not find a declaration file for module 'lodash'. 'lodash/lodash.d.ts' was not found... It's frustrating because the library is clearly there in your node_modules, and the code runs fine in JavaScript. The problem is that TypeScript doesn't just care if the code exists; it needs to know the shape of that code.
Many older or purely JavaScript libraries don't ship with "type definitions" (those .d.ts files). Without them, TypeScript has no idea if _.chunk() expects a number or a string as its second argument, so it treats the whole library as any and throws a warning to let you know you're flying blind.
What exactly is DefinitelyTyped, and how does it relate to those @types packages?
Think of DefinitelyTyped as the world's largest crowdsourced dictionary for JavaScript libraries. It's a massive GitHub repository where the community writes and maintains type definitions for libraries that don't provide their own.
But you don't clone that giant repo into your project. Instead, the DefinitelyTyped project automatically publishes these definitions to npm under the @types scope. When you run npm install @types/lodash, you aren't installing the lodash logic—you're just downloading a small set of .d.ts files that tell TypeScript how lodash works. I like to think of it as a "plugin" for the compiler.
// You install the actual logic here
npm install lodash
// You install the "dictionary" for that logic here
npm install -D @types/lodash
Do I always need to install a separate @types package?
No, and if you try to do it for a library that already includes its own types, you might actually cause version conflicts. Modern libraries—especially those written in TypeScript (like Zod or Axios)—ship their type definitions directly in the main package.
When you install axios, you'll notice you don't need to look for @types/axios. The types are already sitting right there in the root of the package. My rule of thumb? Try installing the library first. If your IDE doesn't show autocomplete or you see that "missing declaration file" error, then go hunting for the @types version.
What happens if a library has no @types package at all?
Every now and then, you'll find a niche library that neither the author nor the community has typed. You have two choices here. If you're in a rush, you can tell TypeScript to stop complaining by creating a declarations.d.ts file in your source folder and adding a "shorthand" declaration:
// declarations.d.ts
declare module 'some-obscure-library';
This essentially tells TypeScript, "Trust me, this module exists, and everything inside it is any." It's not ideal, but it gets you unblocked. If you have the time, the better move is to write a few of your own definitions for the specific functions you're actually using. It's a great way to document the library for the rest of your team.
📋 Practical Task
Resolving Missing Types for Lodash and Date-fns
You are working on a legacy project that uses lodash and date-fns. The project has been migrated to TypeScript, but the developer who did it forgot to install the type definitions. As a result, the build is failing with multiple Could not find a declaration file errors.
Your goal:
- Identify which of the two libraries requires a separate
@typespackage and which one (if any) provides its own types. - Run the correct
npm installcommands to resolve the TypeScript errors for both libraries. - Verify that you can now use
_.partition(from lodash) andformat(from date-fns) with full IntelliSense support in your editor.
Hint: Check the npm registry or the library documentation to see if types are bundled in the main package before installing a @types variant.
There are no comments for now.