Using DefinitelyTyped: Mastering External Type Definitions
Learn how to use DefinitelyTyped to install @types and provide type safety for your JavaScript libraries. Stop guessing types and start coding with confidence.

Previously in this course, we covered mapping API data to local models. While we've focused on typing our own code, real-world development relies on third-party Libraries and Packages. When those packages are written in JavaScript, TypeScript loses its superpower—visibility into what the code does. This lesson shows you how to bridge that gap using DefinitelyTyped.
Understanding Type Definitions
When you install a JavaScript package, TypeScript sees it as an "any" blob. It doesn't know what functions the library exports or what arguments they accept.
DefinitelyTyped is a massive, community-maintained repository of TypeScript definitions. It allows you to add type information to JavaScript packages that don't include it natively. These definitions are published under the @types scope on npm.
How to Install and Use @types
To add type safety for a library, you install its corresponding declaration file. For example, if you are using the popular lodash utility library in your task client, you would run:
Bashnpm install --save-dev @types/lodash
Once installed, TypeScript automatically looks in your node_modules/@types folder. You don't need to import these types; the compiler finds them automatically.
Practical Example: Typing a Library
In our ongoing task API client project, imagine we want to use lodash to group our tasks by status. Without the type definition, TypeScript would complain that _ is implicitly any.
- Install the types:
npm install --save-dev @types/lodash - Use it in your code:
TYPESCRIPTimport _ from CE9178">'lodash'; // Now TypeScript knows that _.groupBy exists and how to use it const tasks = [{ id: 1, status: CE9178">'open' }, { id: 2, status: CE9178">'closed' }]; const grouped = _.groupBy(tasks, CE9178">'status');
Without the @types/lodash package, the compiler would prevent this call or force you to use any, effectively disabling our type-checking for that operation.
Resolving Missing Type Errors
Sometimes, you'll encounter a package that has no @types entry. If you run npm install --save-dev @types/your-package and it fails, it means the community hasn't provided definitions yet.
You have three options:
- Check the library docs: Modern libraries often include their own
.d.tsfiles inside the package itself. - Create a shim: Create a file named
types.d.tsand declare the module yourself (we will cover this in detail in the next lesson). - Use a fallback: As a last resort, you can declare the module as
anyto silence the error:declare module 'your-package';.
Common Pitfalls
- Version Mismatches: If your library version and your
@typesversion are drastically different, you might see "ghost" errors. Always try to keep@typesversions aligned with the major version of the library. - Double Definitions: Never install
@typesfor a library that already includes its own types. Check the library'spackage.jsonfor atypesortypingsfield. - Ignoring the Global scope: Remember that some types are global. You don't need to import them—they are available everywhere once installed.
FAQ
Q: Do I need to include @types in my production build?
A: No. @types packages are only needed by the TypeScript compiler during development. That is why we install them as --save-dev.
Q: Can I use a library if it doesn't have @types? A: Yes, but you lose the safety features of TypeScript. You will be essentially writing standard JavaScript for that specific integration.
Q: Are these definitions always accurate? A: They are maintained by the community. 99% of the time, they are excellent, but occasionally you may find a bug. Because they are open source, you can submit a Pull Request to DefinitelyTyped to fix them!
Recap
Managing dependencies is a core skill for any frontend engineer. By using DefinitelyTyped and the @types ecosystem, you keep your project's type safety consistent, even when utilizing third-party code. Always check for native type support first, then reach for @types, and keep your environment clean by using dev dependencies.
Up next: We will learn how to create your own declaration files with .d.ts files.
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

React & Next.js Dashboard / Admin UI Development
A clean, data-rich dashboard UI in React or Next.js — charts, tables, and real-time data that your users will actually enjoy using.

