TypeScript Definitely Typed
Most of the JavaScript ecosystem on npm was written in plain JavaScript. When you import such a package into a TypeScript project, the compiler has no idea what functions it exports or what types they take. Definitely Typed is the community project that fills that gap: a huge GitHub repository of hand-written type declarations for thousands of JavaScript libraries, published to npm under the @types/ scope.
Where types come from
When you import a package, TypeScript looks for type information in this order:
| Source | Example | What you do |
|---|---|---|
| Types bundled with the package | zod, axios, vue ship .d.ts files |
nothing — it just works |
| Definitely Typed | @types/lodash, @types/express |
npm install --save-dev @types/<name> |
| Your own declarations | a types/legacy-lib.d.ts file |
write a declare module (see Declaration Files) |
| Nothing found | — | error TS7016, or any if noImplicitAny is off |
Many modern packages are written in TypeScript and include their types, so check first. On npmjs.com, a blue TS badge next to the package name means bundled types; a white DT badge means types are available from Definitely Typed.
Installing a type package
Suppose you use lodash, which is plain JavaScript:
npm install lodash
npm install --save-dev @types/lodashadded 1 package, and audited 2 packages in 1s
found 0 vulnerabilities
added 1 package, and audited 3 packages in 1s
found 0 vulnerabilities
Type packages only matter at compile time, so they go in devDependencies. After installing, imports are fully typed:
import { chunk, debounce } from "lodash";
const groups = chunk([1, 2, 3, 4, 5], 2); // number[][]
const save = debounce(() => console.log("saved"), 300);Without the types
If the declarations are missing, strict mode refuses to guess:
src/main.ts(1,24): error TS7016: Could not find a declaration file for module 'lodash'.
'/app/node_modules/lodash/index.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';`The message tells you exactly what to try. The quick fallback — declare module "lodash"; in a .d.ts file — makes everything from the package any, which compiles but throws away all checking. Use it only as a temporary step.
Types for runtimes: @types/node
Definitely Typed also describes environments, not just libraries. Node.js globals such as process, Buffer and modules like node:fs are typed by @types/node. The browser's DOM types, on the other hand, come built into TypeScript through the lib option in tsconfig.json.
{
"compilerOptions": {
"lib": ["ES2023", "DOM"],
"types": ["node"]
}
}types limits which @types/* packages are loaded automatically; leaving it out loads all of them.
What a declaration looks like
Type packages contain only .d.ts files: signatures without implementations. You can write the same kind of declaration inside a playground with declare, which tells TypeScript "this exists at runtime, trust me". Here we describe a pretend global library and then provide the implementation ourselves so the example can run:
declare function slugify(text: string, separator?: string): string;
// In a real project the library provides this; here we fake it:
(globalThis as any).slugify = (text: string, sep = "-") =>
text.toLowerCase().trim().split(/\s+/).join(sep);
console.log(slugify("Hello TypeScript World"));
console.log(slugify("Definitely Typed", "_"));hello-typescript-world
definitely_typed
Because of the declaration, wrong usage is caught even though the compiler never sees the library's code:
declare function slugify(text: string, separator?: string): string;
const slug: number = slugify(42);main.ts(3,7): error TS2322: Type 'string' is not assignable to type 'number'.
main.ts(3,30): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
Note:
@typesversions follow the library's major and minor version. If you useexpress@4, install@types/express@4— mismatched versions are a common source of confusing errors.
Tip: Found a mistake in an
@typespackage? Definitely Typed accepts pull requests on GitHub; fixing the types for a library you use is a great first open-source contribution.
Test yourself
Which command adds the community type declarations for a JS package named uuid?
- npm install uuid-types
- npm install --save-dev @types/uuid
- tsc --types uuid
- npm install typescript/uuid
Do you need an @types package for a library that ships its own .d.ts files?
- Yes, always
- No — bundled types are used automatically
- Only in strict mode
- Only for browser code
What does a .d.ts file contain?
- Compiled JavaScript
- Type declarations without implementations
- Unit tests
- Minified source maps