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-nameFor example:
npm install lodash
npm install --save-dev @types/lodashIf 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 reactYou 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:
typesortypingsfield inpackage.jsonOr fall back to
@types/scoped packages
🛑 Gotchas
Make sure the version of
@types/librarymatches 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.