← Back to TypeScript Course | Chapter 17: TypeScript Configuration | Lesson 4 of 7

Path Aliases

Path aliases give shorter names to frequently used project paths. TypeScript defines these aliases with baseUrl and paths, making imports easier to read and maintain.

baseUrl

baseUrl establishes the base directory TypeScript uses when resolving non-relative module names, turning imports like "utils/helpers" into lookups rooted at that directory instead of requiring "../../utils/helpers".

Example: baseUrl

typescript
// tsconfig.json
// { "compilerOptions": { "baseUrl": "./src" } }
// import { helper } from "utils/helper"; // resolved from src/
console.log("baseUrl roots non-relative imports at a chosen directory");

Defining a Path Alias

The paths option maps an alias pattern to one or more actual project locations, so you can write a short, stable import path instead of a long relative one that breaks every time you move a file.

Example: Defining a Path Alias

typescript
// tsconfig.json
// { "compilerOptions": { "baseUrl": ".", "paths": { "@utils/*": ["src/utils/*"] } } }
// import { helper } from "@utils/helper";
console.log("paths maps a short alias to a real project location");

Using Aliases in Imports

Once configured, TypeScript understands aliased import paths during both type checking and editor autocomplete, so the alias behaves just like a real module path as far as the compiler is concerned.

Example: Using Aliases in Imports

typescript
// import { formatDate } from "@utils/date";
console.log("Aliased imports work in both type checking and editor autocomplete");

Aliases with Multiple Targets

A single alias can point at multiple target patterns, and TypeScript tries each configured substitution in order until one resolves — useful when a symbol might live in more than one possible location.

Example: Aliases with Multiple Targets

typescript
// tsconfig.json
// { "compilerOptions": { "paths": { "@shared/*": ["packages/a/*", "packages/b/*"] } } }
console.log("A single alias can try multiple target locations in order");

Runtime Considerations

TypeScript's paths option only affects how TypeScript itself resolves modules during type checking — it doesn't rewrite the actual runtime import paths, so your bundler or runtime also needs its own alias configuration to match.

Example: Runtime Considerations

typescript
// tsconfig.json's paths only affects TypeScript's own type checking --
// a bundler (webpack, Vite) needs its own matching alias config to run.
console.log("paths doesn't rewrite runtime import paths on its own");
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.