Sub topic 6
How TypeScript finds types for a module
What TypeScript looks for
Prerequisites: you should know how to write import and export, run tsc, and read basic tsconfig.json and package.json files.
When TypeScript sees an import, it must answer two related questions:
- Which JavaScript file will run?
- Which type information describes that file?
The first question is runtime module resolution. The second question is type resolution. TypeScript often uses the same package rules for both, but it can select a declaration file instead of a JavaScript file for type checking.
A declaration file is a file that describes types without containing the implementation. Its filename ends in .d.ts. For an import such as import { greet } from "./greet.js", TypeScript may use greet.ts while compiling the project, or greet.d.ts when consuming a published package.
// greet.d.ts
export declare function greet(name: string): string;The declare keyword says that the function exists at runtime but is implemented somewhere else.
Declaration files in a package
For a package, TypeScript checks several package fields and file names. Modern packages should use an exports map with a types condition. Older packages commonly use a top-level types or typings field.
| Package information | Purpose |
|---|---|
| `exports` with `types` | Selects declarations for a particular import condition |
| `types` or `typings` | Points to the package's main declaration file |
| `index.d.ts` | Common fallback declaration filename |
| `typesVersions` | Provides declarations for selected TypeScript versions |
A package with a simple entry point might contain this configuration:
{
"name": "example-lib",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
}When your code imports example-lib, TypeScript reads dist/index.d.ts for checking. Node.js does not execute that file. Node.js uses the JavaScript entry point instead.
With nodenext, an exports map is more precise because it can describe both ESM and CommonJS consumers:
{
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
}
}The types condition should appear before default in each branch. TypeScript selects the branch that matches the importing file and then reads the declaration path. An ESM importer receives the .d.mts declarations. A CommonJS importer receives the .d.cts declarations.
How file extensions affect declarations
Declaration extensions communicate the module format:
.d.mtsdescribes an ES module..d.ctsdescribes a CommonJS module..d.tsfollows the package and directory module rules.
This distinction matters when a package has different APIs or export shapes in ESM and CommonJS builds.
In a project using nodenext, this source import can resolve to a TypeScript source file during compilation:
// src/main.ts
import { greet } from "./greet.js";
console.log(greet("world"));The .js extension describes the emitted file that Node.js will load. TypeScript can associate it with src/greet.ts while checking the source project.
Common failures and fixes
If TypeScript reports that it cannot find declarations for a package, check these points:
- The package actually publishes its
.d.ts,.d.mts, or.d.ctsfiles. - The
typespath is correct relative topackage.json. - The
exportsmap exposes the declaration path. - The selected
importorrequirebranch has atypesentry. - Your
moduleandmoduleResolutionsettings match the package format. - You are not relying on
skipLibCheckto solve a missing file. That option skips checking existing library declarations; it does not create declarations.
The compiler can show its search process:
npx tsc --noEmit --traceResolution
npx tsc --noEmit --explainFilestraceResolution prints attempted module and declaration paths. explainFiles explains why files became part of the compilation.
Exercise
Exercise
A package has "exports": { ".": { "import": "./dist/index.mjs", "types": "./dist/index.d.ts" } }. What is the main problem?
Show answer
The types condition is outside the import branch, so it does not clearly describe the ESM entry. Put "types": "./dist/index.d.ts" inside the "import" branch, before "default" when both are present.