Archived post: posted sometime between 2016 and 2022.

TypeScript module resolution fundamentals

Tracing

Use the --traceResolution compiler option to debug module resolution.

Classic (legacy) Module Resolution

This is available for backward compatibility.

Node (default) Module Resolution

./root/src/moduleA.ts will resolve import { B } from "./moduleB" as follows:

It will resolve import { B } from "moduleB" similarly, but will look inside node_modules starting on the same level as moduleA and then walking up the directory tree.

Note also that .ts may also be the .tsx or .d.ts suffixes.

Ambient Modules

Ambient modules allows us to declare more than one module per file. We declare the module name in quotes and put the module inside brackets.

/// fooBarBaz.d.ts

declare module "foo" {
    export class SomeClass {}
}

declare module "bar" {
    export class OtherClass {}
}

declare module "baz" {
    export class AnotherClass {}
}

Relative Imports

If it starts with ./ ../ or / then it is a relative import.

Non-relative Imports

See https://www.typescriptlang.org/docs/handbook/module-resolution.html