External Libraries
Installing and Using @types
TypeScript uses the DefinitelyTyped repository to provide type definitions for JavaScript libraries that do not include their own types.
- Many JavaScript libraries do not include TypeScript types by default.
- The `@types` namespace provides community-maintained type definitions.
- Installing `@types/library-name` enables TypeScript to recognise types for third-party libraries.
// Importing lodash without type definitions results in an error
import _ from "lodash"; // Type error if types are missing
// Install types for lodash
$ npm install --save-dev @types/lodash
// Now TypeScript recognises lodash methods
const numbers: number[] = [1, 2, 3];
console.log(_.shuffle(numbers)); // Works with type safety