TExcelTAD
Home › TypeScript › TS Definitely Typed

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:

bash · try it yourself
npm install lodash
npm install --save-dev @types/lodash
added 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:

ts
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:

text
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.

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:

ts · try it yourself
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:

ts · try it yourself
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: @types versions follow the library's major and minor version. If you use express@4, install @types/express@4 — mismatched versions are a common source of confusing errors.

Tip: Found a mistake in an @types package? 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?

  1. npm install uuid-types
  2. npm install --save-dev @types/uuid
  3. tsc --types uuid
  4. npm install typescript/uuid

Do you need an @types package for a library that ships its own .d.ts files?

  1. Yes, always
  2. No — bundled types are used automatically
  3. Only in strict mode
  4. Only for browser code

What does a .d.ts file contain?

  1. Compiled JavaScript
  2. Type declarations without implementations
  3. Unit tests
  4. Minified source maps