← Back to TypeScript Course | Chapter 11: Type Declarations and DefinitelyTyped | Lesson 3 of 5

Using @types packages

@types packages provide TypeScript declarations for JavaScript libraries that do not include their own types. They allow TypeScript to provide checking and editor support for those libraries.
Syntax
typescript
npm install --save-dev @types/package-name

What @types Packages Are

@types packages are community-maintained declaration files published separately on npm for JavaScript libraries that don't ship their own TypeScript types, hosted collectively under the DefinitelyTyped project.

Example: What @types Packages Are

typescript
// npm install --save-dev @types/lodash
// Provides types for the lodash JS library, published under DefinitelyTyped.
console.log("@types packages add compile-time types for untyped JS libraries");

Installing a Type Package

Installing a type package (like npm install --save-dev @types/lodash) adds nothing to your runtime bundle — it only adds compile-time type information that TypeScript picks up automatically alongside the library it types.

Example: Installing a Type Package

typescript
// npm install --save-dev @types/express
// Adds zero runtime code -- only type information used by the compiler.
console.log("Installing @types/express adds no runtime bundle weight");

Using Typed Library APIs

Once a @types package is installed, calling into that library's functions gets full autocomplete and type-checking as if the library had been written in TypeScript from the start, catching mismatched arguments before you run anything.

Example: Using Typed Library APIs

typescript
// After installing @types/lodash:
// import _ from "lodash";
// _.chunk([1, 2, 3], 2); // fully typed and autocompleted
console.log("Typed library calls get full autocomplete and checking");

Libraries with Built-In Types

Some libraries ship their own types bundled directly in their package (often noted by a types or typings field in their package.json), which means installing a separate @types package for them isn't necessary or even possible.

Example: Libraries with Built-In Types

typescript
// axios ships its own types (see its package.json "types" field),
// so `npm install --save-dev @types/axios` is unnecessary.
console.log("Some libraries bundle their own types, skip a separate @types install");

Checking Type Package Availability

Before assuming you need a separate @types install, check the library's own package for bundled types first — installing a redundant @types package for an already-typed library can create conflicting type definitions.

Example: Checking Type Package Availability

typescript
// Check package.json for a "types" or "typings" field first.
// Installing a redundant @types package can create conflicting definitions.
console.log("Always check for bundled types before installing @types separately");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Installing @types/lodash without installing lodash itself, since the types package has no runtime code.
  2. Adding @types packages to dependencies for production when they are only needed as devDependencies.
  3. Installing @types/x for a library that already ships its own types, which can cause conflicts.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.