How typescript finds the location of type declaration file
TypeScript uses several mechanisms to find and load type declaration files automatically. When you import a module, TypeScript follows a series of steps to locate the appropriate type definitions. Here's an overview of how TypeScript finds declaration files:
Steps to Locate Declaration Files
-
TypeScript Built-in Definitions:
- TypeScript includes built-in type definitions for the standard JavaScript environment (like
Array,Map, etc.). These are always available without any additional configuration.
- TypeScript includes built-in type definitions for the standard JavaScript environment (like
-
Triple-Slash Directives:
- TypeScript files can include triple-slash directives (
/// <reference path="..." />) to explicitly reference other declaration files.
- TypeScript files can include triple-slash directives (
-
typesortypingsField inpackage.json:- When a package includes type definitions, it often specifies them in the
typesortypingsfield of itspackage.jsonfile. For example:{ "name": "my-package", "version": "1.0.0", "types": "index.d.ts" }
- When a package includes type definitions, it often specifies them in the
-
@types Packages:
- For many popular JavaScript libraries, the type definitions are maintained separately in the DefinitelyTyped repository and distributed via
@typespackages. When you install a package likelodash, you can install its type definitions with:npm install --save-dev @types/lodash - TypeScript automatically looks for
@typespackages in thenode_modules/@typesdirectory.
- For many popular JavaScript libraries, the type definitions are maintained separately in the DefinitelyTyped repository and distributed via
-
Declaration Files in the Module:
- If the module includes its own
.d.tsfiles, TypeScript will use those. These are typically located in the root of the module or specified by thetypesfield inpackage.json.
- If the module includes its own
-
node_modulesResolution:- TypeScript follows the Node.js module resolution strategy. When you import a module, TypeScript looks for type definitions in the following order:
- Look for a
package.jsonfile in the module root and check thetypesortypingsfield. - Look for an
index.d.tsfile in the module root. - Traverse up the directory tree, looking for
node_modulesdirectories and repeating the previous steps.
- Look for a
- TypeScript follows the Node.js module resolution strategy. When you import a module, TypeScript looks for type definitions in the following order:
Example: Locating Lodash Type Definitions
When you import Lodash in your TypeScript file like this:
import _ from 'lodash';
TypeScript will follow these steps:
-
Check for a
lodashPackage:- Look in
node_modules/lodash/package.jsonfor atypesortypingsfield. - Lodash does not include its own type definitions, so this step does not yield results.
- Look in
-
Check for
@typesPackage:- Look in
node_modules/@types/lodashfor type definitions. - The
@types/lodashpackage provides the type definitions for Lodash.
- Look in
-
Use the Type Definitions:
- Once TypeScript finds the type definitions in
node_modules/@types/lodash, it uses them to provide type checking and IntelliSense for Lodash.
- Once TypeScript finds the type definitions in
Installing and Using @types Packages
To ensure TypeScript finds and uses the correct type definitions, you install the @types package corresponding to the module:
npm install lodash
npm install --save-dev @types/lodash
or
yarn add lodash
yarn add @types/lodash --dev
Published on: Jul 28, 2024, 06:30 AM