Module resolution and package types
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Explain node16, bundler and classic resolution.
- Use paths and the types field correctly.
- Diagnose a missing type declaration.
Overview
#Resolution modes
classicis legacy and rarely used.nodeandnode10mimic older Node resolution.node16andnodenexthonor theexportsfield and require explicit file extensions in ESM.bundlermatches what Vite and esbuild do and allows extensionless imports.
The types field
A package can point to its declarations.
{
"types": "./dist/index.d.ts",
"exports": {
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" }
}
}
With exports, the types condition must come first in each entry.
Path aliases
{ "compilerOptions": { "baseUrl": ".", "paths": { "@app/*": ["src/*"] } } }
The bundler must be told the same alias; tsconfig alone does not rewrite imports.
Missing types
Could not find a declaration file for module 'x' means the package ships no types. Install @types/x, add a local .d.ts, or set allowJs and checkJs.
moduleResolution and module go together
Choose module: "esnext" with moduleResolution: "bundler" for a Vite app; choose nodenext for a Node library.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which `moduleResolution` matches Vite and allows extensionless imports?
Exercise 2Implement
Write the `exports` entry that exposes types and an ESM import for a package.
starter.json{ "exports": { // your code } }Reveal the reference answer
reference.json{ "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" } } }Authored expected output: Compile-time: consumers resolve types from the types condition. No runtime output.
The types condition must come before import so the checker finds declarations first.
Exercise 3Debug
This import fails with "Could not find a declaration file". Give two fixes. ```ts import legacy from "legacy-lib"; ```
starter.typescriptimport legacy from "legacy-lib";Reveal the reference answer
reference.typescriptInstall the community types with `npm i -D @types/legacy-lib`, or add a local declaration file: declare module "legacy-lib" { const value: unknown; export default value; }Authored expected output: Compile error resolved: either @types/legacy-lib or a local legacy-lib.d.ts supplies the missing types.
A declaration file tells the checker about the JavaScript module's shape.
Common misconceptions
#- tsconfig paths rewrite emitted imports.
- node resolution reads the exports field.
- A missing types error means the package is broken.
Follow-up practice
#- typescript-decorators
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.