Skip to Content
Course content

162: DefinitelyTyped and @types Packages

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 @types package and which one (if any) provides its own types.
  • Run the correct npm install commands to resolve the TypeScript errors for both libraries.
  • Verify that you can now use _.partition (from lodash) and format (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.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.