Sometimes TypeScript needs extra information about JavaScript libraries to understand how to use them safely. DefinitelyTyped and @types packages give TypeScript this information.
DefinitelyTyped and @types packages in Typescript
npm install --save-dev @types/library-name
The @types packages contain type definitions for JavaScript libraries.
They are usually installed as development dependencies because they help during coding but are not needed at runtime.
lodash library.npm install --save-dev @types/lodash
react library, so TypeScript understands React code.npm install --save-dev @types/react
npm install --save-dev @types/node
This program uses the lodash library to double numbers in an array. Installing @types/lodash helps TypeScript understand the _.map function and check types.
import _ from 'lodash'; const numbers: number[] = [1, 2, 3, 4]; const doubled = _.map(numbers, n => n * 2); console.log(doubled);
DefinitelyTyped is a big collection of type definitions maintained by the community.
When you install @types/library-name, you get the types from DefinitelyTyped for that library.
If a library already includes its own types, you don't need to install @types separately.
DefinitelyTyped provides type definitions for many JavaScript libraries.
@types packages let you add these types to your TypeScript projects easily.
Using them helps TypeScript check your code and gives better editor support.