Skip to article content
FrontendXLesson 37 / 40

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.

intermediate · about 45 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

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

  • classic is legacy and rarely used.
  • node and node10 mimic older Node resolution.
  • node16 and nodenext honor the exports field and require explicit file extensions in ESM.
  • bundler matches what Vite and esbuild do and allows extensionless imports.

The types field

A package can point to its declarations.

snippet.json
{
  "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

snippet.json
{ "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.

  1. Exercise 1Predict

    Which `moduleResolution` matches Vite and allows extensionless imports?

    Choose the best answer
  2. 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.

  3. Exercise 3Debug

    This import fails with "Could not find a declaration file". Give two fixes. ```ts import legacy from "legacy-lib"; ```

    starter.typescript
    import legacy from "legacy-lib";
    

    Reveal the reference answer
    reference.typescript
    Install 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

Related resources

#

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.

© 2026 FrontendX. All rights reserved.