Every npm package you install in a JavaScript project becomes a potential type problem in TypeScript. Some packages ship their own type definitions. Some have community-maintained types on DefinitelyTyped. Some have nothing. Knowing where to look, what to install, and what to do when nothing exists saves hours of confusion during migration.
How TypeScript finds types for npm packages
TypeScript looks for type definitions in two places:
-
Inside the package itself. Many modern packages include a
typesortypingsfield in theirpackage.jsonpointing to a bundled.d.tsfile. If this exists, installing the package is enough — no separate install needed. -
In the
@typesscope on npm. The DefinitelyTyped project maintains community type definitions for packages that don't bundle their own. You install these separately:npm install --save-dev @types/package-name.
You can tell which situation applies by running npm info <package> types typings after installing the package. If it returns a path like dist/index.d.ts, the types are bundled. If it returns nothing, you need @types.
Packages that bundle their own types
These packages need no additional installation — their types are included:
| Package | Notes |
|---|---|
@playwright/test | TypeScript-first, ships complete .d.ts files |
cypress | Types included since Cypress 10 |
axios | Ships bundled types |
zod | Ships bundled types |
@faker-js/faker | Ships bundled types (use this, not the older faker package) |
ts-jest | Ships bundled types |
Common @types packages for QA projects
These packages require a separate @types install:
# Always install for Node.js projects
npm install --save-dev @types/node
# Test frameworks
npm install --save-dev @types/jest # if using Jest without ts-jest
npm install --save-dev @types/mocha # Mocha
npm install --save-dev @types/chai # Chai assertions
# Utilities
npm install --save-dev @types/lodash
npm install --save-dev @types/uuid
npm install --save-dev @types/jsonwebtoken
npm install --save-dev @types/cookie
npm install --save-dev @types/supertest # API testing with supertestHow to check if @types exists
Three ways, fastest first:
Option 1 — try installing it:
npm install --save-dev @types/package-name
# If the package doesn't exist, npm prints a 404 errorOption 2 — search on npm:
npm search @types/package-nameOption 3 — the TypeScript type search tool. The TypeScript website hosts a search at typescriptlang.org/dt/search that queries the entire DefinitelyTyped repository.
Version matching
@types packages follow the major version of the package they describe. If you install lodash@4.17.21, you want @types/lodash@4.x.x — not @types/lodash@5.x.x.
# Check your lodash version
npm list lodash
# lodash@4.17.21
# Install matching @types
npm install --save-dev @types/lodash@^4A major version mismatch produces errors like Property X does not exist on type Y where the method exists in the library but the type definition predates or postdates its addition.
Reading type definitions
Once types are installed, VS Code lets you inspect them directly. Cmd+Click (Mac) or Ctrl+Click (Windows) on any function name from an installed package opens the .d.ts file. This is useful when:
- The autocomplete shows a function but you're not sure of its full signature
- A type error says "Expected X" and you want to see what X actually is
- You need to extend an existing type and want to see its current shape
For example, clicking into @types/jest shows the full signature for expect, describe, beforeEach, and every matcher — saving you from reading the Jest docs when you're unsure of a method's type.
The "types" field in tsconfig
By default, TypeScript auto-includes every @types package installed in node_modules/@types. If your tsconfig.json has an explicit "types" array, only the listed packages are included:
{
"compilerOptions": {
"types": ["cypress", "node"]
}
}This is intentional in Cypress projects — it prevents browser DOM types from conflicting with Cypress's DOM definitions. But it also means @types/lodash, @types/chai, or any other installed package won't be auto-included. Add them to the array if you use them in that tsconfig's scope.
Handling a package with outdated @types
Sometimes @types/package-name is far behind the package's current version — missing methods, wrong signatures. You'll see errors like:
Property 'newMethod' does not exist on type 'SomeClass'
Even though newMethod was added in version 3.1 and you're running 3.2. Options:
- Pin the package to the last version the
@typescovers —npm install package-name@<last-covered-version>. - Augment the missing types yourself — add the missing method to the library's interface via module augmentation (covered in Lesson 3 of this chapter).
- Open a pull request to DefinitelyTyped — if you have time and the package is widely used, the community benefits from the fix.
For a migration, option 2 is usually the fastest unblock.
⚠️ Common mistakes
- Installing
@typesas a regular dependency instead of--save-dev. Type definitions are a development tool. They don't need to be in your production bundle. Alwaysnpm install --save-dev @types/.... - Installing both bundled types and a
@typespackage. For packages that ship their own types (Playwright, Axios), installing@types/playwrightor@types/axiosintroduces a second set of type definitions that can conflict. Check whether the package bundles types before reaching for@types. - Assuming
@typespackages are always correct. DefinitelyTyped is community-maintained and sometimes lags behind or contains errors. If you get a type error that seems wrong — the method clearly exists and works — read the.d.tssource to verify.
🎯 Practice task
Audit your project's npm dependencies for type coverage.
- Open
package.jsonand list all dependencies independenciesanddevDependencies. - For each package, check whether it bundles types:
npm info <package> types typings. Note the result. - For packages without bundled types, check if
@types/<package>exists on npm. - Install any missing
@typespackages you find. - Run
npm run type-check. Did any new type errors appear now that TypeScript knows the correct signatures? Fix them. - Stretch: pick one installed
@typespackage. Open the.d.tsfile (Cmd+Click on any function from that package in VS Code). Find the interface or type that describes the main export. Read its full shape. This is a skill you'll use repeatedly during migration — understanding the type you're working with, not just the error message.