Typescript Foundations / Type Declaration and third party types

Installing types from definitely typed

In TypeScript, when using a JavaScript library (like lodash, express, etc.), it often doesn't include type definitions by default. That’s where DefinitelyTyped comes in.


🧠 What is DefinitelyTyped?

DefinitelyTyped is a community-driven project that provides TypeScript type definitions for popular JavaScript libraries.

These types are published under the NPM scope @types.


✅ Installing Types from DefinitelyTyped

You install them like this:

npm install --save-dev @types/package-name

For example:

npm install lodash
npm install --save-dev @types/lodash

If you're using yarn:

yarn add lodash
yarn add --dev @types/lodash

📌 When You Don’t Need to Install @types

Many libraries are written in TypeScript and ship with built-in types, e.g.:

npm install react

You do not need to install @types/react because it's already included.

But if the library doesn’t include types (like uuid), then you do:

npm install uuid
npm install --save-dev @types/uuid

🧩 Where TypeScript Looks for Types

TypeScript will automatically look for:

  • types or typings field in package.json

  • Or fall back to @types/ scoped packages


🛑 Gotchas

  • Make sure the version of @types/library matches the version of the library.

  • If no types exist on DefinitelyTyped, you may have to write your own .d.ts.


🔧 Example Use Case

import _ from "lodash";

const result = _.chunk([1, 2, 3, 4], 2); // TypeScript knows result is number[][]

Thanks to @types/lodash, TypeScript understands what _.chunk() returns.