Lesson 3 of 8
Path Aliases - Clean Imports
Path aliases let you write `import { Task } from '@/types/domain'` instead of `import { Task } from '../../../../types/domain'`. They're configured in `tsconfig.json` and require matching configuration in your build tool (Vite, webpack, Jest).
Path Aliases - tsconfig + Build Tool Setup
// ── 1. tsconfig.json - define aliases ──────────────────────────
// "compilerOptions": {
// "baseUrl": "./src",
// "paths": {
// "@/*": ["./*"], // @/utils/log = src/utils/log
// "@types/*": ["./types/*"],
// "@services/*": ["./services/*"],
// "@lib/*": ["./lib/*"],
// "@components/*":["./components/*"]
// }
// }
// ── 2a. Vite configuration - vite.config.ts ─────────────────
import { defineConfig } from "vite";
import path from "path";
export default defineConfig({
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
"@types": path.resolve(__dirname, "./src/types"),
"@services": path.resolve(__dirname, "./src/services"),
"@lib": path.resolve(__dirname, "./src/lib"),
"@components": path.resolve(__dirname, "./src/components"),
},
},
});
// ── 2b. Node.js / ts-node - tsconfig-paths ───────────────────
// npm install --save-dev tsconfig-paths
// In package.json scripts or ts-node:
// ts-node -r tsconfig-paths/register src/index.ts
// OR in tsconfig:
// "ts-node": { "require": ["tsconfig-paths/register"] }
// ── 2c. Jest - moduleNameMapper ──────────────────────────────
// jest.config.ts
// {
// moduleNameMapper: {
// "^@/(.*)$": "<rootDir>/src/$1",
// "^@types/(.*)$": "<rootDir>/src/types/$1",
// }
// }
// ── Usage - clean imports everywhere ────────────────────────
// ❌ Before:
import { Task } from "../../../../types/domain";
import { TaskService } from "../../../services/taskService";
// ✅ After:
import type { Task } from "@/types/domain";
import { TaskService } from "@/services/taskService";
import { EventBus } from "@/lib/eventBus";Common Mistakes
- Setting paths in tsconfig but forgetting the build tool - TypeScript resolves aliases for type checking only. At runtime, the aliases need to be resolved by Vite/webpack/tsconfig-paths. Both must be configured.
- Using deep aliases (`@components/TaskCard/TaskCard.component`) instead of shallow one - keep aliases to the `src/` root or major directories. Deep aliases create maintenance burden.
- Forgetting Jest - when adding aliases, update `moduleNameMapper` in `jest.config.ts` or tests will fail with 'cannot find module @/...' even though the app builds fine.
Tip
Tip
Practice Path Aliases Clean Imports in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Path Aliases - Clean Imports
Practice Task
Note
Practice Task - (1) Write a working example of Path Aliases Clean Imports from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Path Aliases Clean Imports is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- Path aliases let you write `import { Task } from '@/types/domain'` instead of `import { Task } from '.
- Setting paths in tsconfig but forgetting the build tool - TypeScript resolves aliases for type checking only. At runtime, the aliases need to be resolved by Vite/webpack/tsconfig-paths. Both must be configured.
- Using deep aliases (`@components/TaskCard/TaskCard.component`) instead of shallow one - keep aliases to the `src/` root or major directories. Deep aliases create maintenance burden.
- Forgetting Jest - when adding aliases, update `moduleNameMapper` in `jest.config.ts` or tests will fail with 'cannot find module @/...' even though the app builds fine.
Finished this lesson?