Skip to content
tezvyn:
Menu

DefinitelyTyped: Type Definitions for JavaScript Libraries

Source: definitelytyped.orgEasyHow cards are made

DefinitelyTyped: Type Definitions for JavaScript Libraries

@types packages from DefinitelyTyped are instruction manuals for JavaScript libraries, letting TypeScript understand their shapes. You install them for JS libs that lack their own types, enabling autocompletion.

Why it exists

TypeScript provides safety by knowing the "type" of every variable. But most of the JavaScript ecosystem was written without types. To use these libraries safely, TypeScript needs a description of their APIs, which is what the DefinitelyTyped project provides.

The mental model

Think of @types packages as a Rosetta Stone for JavaScript libraries. They don't contain any functional code, only "declaration files" (.d.ts) that translate the library's JavaScript functions, objects, and values into a format TypeScript can understand and type-check.

How it works

When you install a library like lodash, the TypeScript compiler looks for its type definitions. If lodash doesn't include them, the compiler checks for a companion package, @types/lodash. If you've installed it, TypeScript uses those files to provide autocompletion and catch errors, like passing a string where a number is expected. This vast collection of type definitions is managed by the community in the DefinitelyTyped GitHub repository.

When to use it

You need an @types package whenever your TypeScript compiler or IDE complains it "Cannot find module ... or its corresponding type declarations" for a JavaScript library you've installed. This is your cue to run npm install --save-dev @types/the-library-name.

When not to use it

Avoid installing an @types package for a library that now includes its own types. Many modern libraries are written in TypeScript or bundle their own .d.ts files. Check the library's package.json for a "types" or "typings" field. Installing a redundant @types package can cause type conflicts and build errors.

One canonical example

To use the express web server in a TypeScript project, you first run npm install express. Your code import express from 'express'; will cause a TypeScript error. To fix it, you install the type definitions: npm install --save-dev @types/express. Now, TypeScript knows that express() returns an Application object and can provide accurate autocompletion and type checking for methods like app.get(...).

Interview question

When should you install an @types package for a JavaScript library?

  • a.For every JavaScript library you use, to ensure maximum type safety.
  • b.To provide type definitions for a JavaScript library that doesn't include its own.Correct
  • c.When the library is written in TypeScript and needs to be compiled.
  • d.To update the functional code of a JavaScript library to its latest version.
Why?

The card states you need an @types package when a JavaScript library doesn't include its own type definitions, allowing TypeScript to understand its API. Installing @types for every library (A) is incorrect because it can cause conflicts if the library already bundles its own types.

Just read this? Test yourself on what you have been reading.

Read the original → definitelytyped.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles