Course contentsShow
← TypeScript
Lesson 1131 of 1,79626. Modules in TypeScriptFree lesson

DefinitelyTyped and @types Packages

Using community-maintained type definitions from @types scope and contributing to DefinitelyTyped.

DefinitelyTyped and @types Packages

You'll learn how to use community-maintained type definitions for JavaScript libraries and understand the ecosystem that makes TypeScript work with the JavaScript world.

The Problem: JavaScript Libraries Don't Have Built-In Types

When you install a JavaScript library like lodash, express, or react, these packages were written in plain JavaScript. They work perfectly fine at runtime, but TypeScript doesn't know about their types. Without type information, you lose autocomplete, type checking, and all the benefits TypeScript provides.

Think of it like receiving furniture from another country with instructions in a language you don't speak. The furniture works fine, but you need a translation guide.

The DefinitelyTyped Repository

DefinitelyTyped is a massive, community-maintained GitHub repository that hosts type definitions for thousands of JavaScript libraries. It's one of the largest open-source projects on GitHub, with contributions from thousands of developers worldwide.

When library authors write packages in JavaScript (not TypeScript), volunteers create matching .d.ts declaration files that describe the library's API. These files are then published to npm under the @types scope.

Installing @types Packages

When you need types for a JavaScript library, install the corresponding @types package:

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

The naming convention is simple: for a package named xyz, install @types/xyz. The @types package only contains type definitions—no runtime code—so it's installed as a dev dependency.

How TypeScript Finds These Types

TypeScript automatically looks in node_modules/@types when resolving types. You don't need special configuration. When you import lodash, TypeScript automatically finds and uses the types from @types/lodash.

This happens because of the types field behavior in package.json and TypeScript's default module resolution settings.

Contributing to DefinitelyTyped

If you find incorrect or missing types, you can contribute! The repository accepts pull requests to fix bugs, add missing definitions, or create types for new libraries. The community maintains quality through reviews and automated testing.

Key Takeaway: DefinitelyTyped provides community-maintained type definitions published as @types packages, allowing TypeScript to understand JavaScript libraries—just install @types/package-name alongside any untyped JavaScript library.