Course contentsShow
← TypeScript
Lesson 34 of 1,7961. Why TypeScriptFree lesson

TypeScript in Package Ecosystems

How npm packages ship types via DefinitelyTyped or bundled declarations.

TypeScript in Package Ecosystems

What you'll learn: How npm packages provide TypeScript type information so you get autocomplete and error checking when using them.

The Problem: JavaScript Libraries Without Types

When you install a JavaScript library from npm (like lodash or axios), TypeScript doesn't automatically know what functions exist or what arguments they accept. Without type information, you lose all the benefits of static typing when using these packages.

Two Solutions for Package Types

1. DefinitelyTyped (@types packages)

For JavaScript libraries that don't include their own types, the community maintains type definitions in a massive repository called DefinitelyTyped. When you need types for a package like lodash, you install a separate companion package:

npm install lodash
npm install --save-dev @types/lodash

The @types/lodash package contains type declarations that describe the library's API. TypeScript automatically finds and uses these.

2. Bundled Type Declarations

Modern packages often ship their own types directly. Libraries written in TypeScript (or those that added types later) include .d.ts files alongside their JavaScript code. When you install the package, the types come with it—no extra step needed.

You can tell if a package bundles types by checking for a types or typings field in its package.json.

Why This Matters

This ecosystem approach means TypeScript works seamlessly with the entire npm registry. You get autocomplete, type checking, and documentation for thousands of libraries, whether they were originally written in TypeScript or not.

Key Takeaway: npm packages provide TypeScript types either through separate @types/* packages from DefinitelyTyped or by bundling their own type declarations, giving you full type safety across the entire JavaScript ecosystem.