Skip to main content

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).

9 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Path Aliases - tsconfig + Build Tool Setup

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.

Diagram
Loading diagram…

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.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done