+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Part 129 of 355

๐Ÿ—บ ๏ธ Path Mapping: Custom Module Paths

Master TypeScript's path mapping feature to create clean, maintainable import statements with custom module resolution paths and aliases ๐Ÿš€

๐Ÿš€Intermediate
28 min read

Prerequisites

  • Understanding of TypeScript module resolution basics ๐Ÿ“
  • Knowledge of tsconfig.json configuration options โšก
  • Experience with relative and absolute import statements ๐Ÿ’ป

What you'll learn

  • Master TypeScript's path mapping configuration and syntax ๐ŸŽฏ
  • Create clean, maintainable import statements with custom aliases ๐Ÿ—๏ธ
  • Implement advanced path mapping patterns for complex projects ๐Ÿ›
  • Optimize development workflow with intelligent path resolution โœจ

๐ŸŽฏ Introduction

Welcome to the cartographerโ€™s world of path mapping! ๐Ÿ—บ๏ธ If long, messy import statements like ../../../components/ui/Button are tangled jungle paths, then TypeScriptโ€™s path mapping is your GPS system - it transforms those confusing relative routes into clean, intuitive shortcuts like @/components/ui/Button!

Path mapping is one of TypeScriptโ€™s most powerful features for improving code maintainability and developer experience. It allows you to define custom aliases for module paths, making imports shorter, more readable, and less brittle when you restructure your project. Whether youโ€™re building a small application or managing a massive enterprise codebase, mastering path mapping will dramatically improve your development workflow.

By the end of this tutorial, youโ€™ll be a path mapping expert, capable of creating sophisticated import alias systems that make your code cleaner, more maintainable, and a joy to work with. Letโ€™s navigate the world of custom module paths! ๐ŸŒŸ

๐Ÿ“š Understanding Path Mapping

๐Ÿค” What Is Path Mapping?

Path mapping allows you to define custom aliases for module paths in your TypeScript configuration. Instead of using relative paths that can become unwieldy as your project grows, you can create meaningful shortcuts that remain consistent regardless of where the importing file is located.

// ๐ŸŒŸ Before path mapping - relative import hell
import { UserService } from '../../../services/user/UserService';
import { Button } from '../../../../components/ui/Button/Button';
import { ValidationUtils } from '../../../utils/validation/ValidationUtils';
import { ApiTypes } from '../../../../types/api/ApiTypes';
import { Constants } from '../../../config/constants/Constants';

// โœจ After path mapping - clean and intuitive
import { UserService } from '@services/user/UserService';
import { Button } from '@components/ui/Button/Button';
import { ValidationUtils } from '@utils/validation/ValidationUtils';
import { ApiTypes } from '@types/api/ApiTypes';
import { Constants } from '@config/constants/Constants';

// ๐ŸŽฏ Even better with organized aliases
import { UserService } from '@/services/user/UserService';
import { Button } from '@/components/ui/Button';
import { ValidationUtils } from '@/utils/validation';
import type { ApiTypes } from '@/types/api';
import { Constants } from '@/config/constants';

// ๐Ÿ”ง Path mapping configuration in tsconfig.json
interface PathMappingConfig {
  compilerOptions: {
    baseUrl: string;                    // Base directory for path resolution
    paths: Record<string, string[]>;    // Path mapping patterns
    rootDirs?: string[];               // Additional root directories
  };
}

// ๐Ÿ“ Comprehensive path mapping example
const comprehensivePathMapping: PathMappingConfig = {
  compilerOptions: {
    baseUrl: "./src",                   // All paths relative to src/
    paths: {
      // ๐ŸŽฏ Root alias - map @ to src root
      "@/*": ["*"],
      
      // ๐Ÿ“ฆ Feature-based aliases
      "@components/*": ["components/*"],
      "@services/*": ["services/*"],
      "@utils/*": ["utils/*"],
      "@hooks/*": ["hooks/*"],
      "@pages/*": ["pages/*"],
      "@layouts/*": ["layouts/*"],
      
      // ๐ŸŽจ Asset and style aliases
      "@assets/*": ["assets/*"],
      "@styles/*": ["styles/*"],
      "@images/*": ["assets/images/*"],
      "@icons/*": ["assets/icons/*"],
      
      // ๐Ÿ”ง Configuration and types
      "@config/*": ["config/*"],
      "@types/*": ["types/*"],
      "@constants/*": ["constants/*"],
      
      // ๐Ÿงช Testing utilities
      "@test-utils/*": ["../test-utils/*"],
      "@mocks/*": ["../test/mocks/*"],
      
      // ๐ŸŒ Environment-specific paths
      "@env": ["config/environments/production"],
      "@env/*": ["config/environments/*"],
      
      // ๐Ÿ“š External library aliases
      "react": ["../node_modules/@types/react"],
      "lodash": ["../node_modules/@types/lodash"],
      
      // ๐ŸŽฏ Specific module aliases
      "api": ["services/api/index"],
      "database": ["services/database/index"],
      "auth": ["services/auth/index"]
    },
    
    // ๐Ÿ—‚๏ธ Multiple root directories for complex projects
    rootDirs: [
      "./src",
      "./generated",
      "./shared"
    ]
  }
};

// ๐Ÿ—๏ธ Advanced path mapping manager
class PathMappingManager {
  private mappings: Map<string, string[]> = new Map();
  private baseUrl: string = '.';
  private resolutionCache: Map<string, string> = new Map();

  constructor(config: { baseUrl?: string; paths?: Record<string, string[]> }) {
    this.baseUrl = config.baseUrl || '.';
    
    if (config.paths) {
      Object.entries(config.paths).forEach(([pattern, substitutions]) => {
        this.mappings.set(pattern, substitutions);
      });
    }
    
    console.log('๐Ÿ—บ๏ธ Path Mapping Manager initialized');
    console.log(`๐Ÿ“ Base URL: ${this.baseUrl}`);
    console.log(`๐Ÿ“‹ Mappings: ${this.mappings.size} patterns configured`);
  }

  // ๐Ÿ” Resolve import specifier using path mappings
  resolveImport(importSpecifier: string, fromFile?: string): {
    resolved: string | null;
    usedPattern: string | null;
    substitution: string | null;
    steps: string[];
  } {
    const steps: string[] = [];
    steps.push(`๐Ÿ” Resolving import: "${importSpecifier}"`);
    
    // ๐Ÿงฒ Check cache first
    const cacheKey = `${importSpecifier}:${fromFile || 'global'}`;
    if (this.resolutionCache.has(cacheKey)) {
      const cached = this.resolutionCache.get(cacheKey)!;
      steps.push(`๐Ÿ’พ Found in cache: ${cached}`);
      return {
        resolved: cached,
        usedPattern: null,
        substitution: null,
        steps
      };
    }

    // ๐Ÿ—บ๏ธ Try each path mapping pattern
    for (const [pattern, substitutions] of this.mappings) {
      steps.push(`๐Ÿ” Trying pattern: ${pattern}`);
      
      const match = this.matchPattern(pattern, importSpecifier);
      if (match) {
        steps.push(`โœ… Pattern matched with groups: [${match.groups.join(', ')}]`);
        
        // ๐Ÿ”„ Try each substitution
        for (const substitution of substitutions) {
          const resolvedPath = this.applySubstitution(substitution, match.groups);
          const fullPath = this.resolveRelativeToBase(resolvedPath);
          
          steps.push(`๐Ÿ”„ Trying substitution: ${substitution} -> ${fullPath}`);
          
          // ๐Ÿ’พ Cache successful resolution
          this.resolutionCache.set(cacheKey, fullPath);
          
          return {
            resolved: fullPath,
            usedPattern: pattern,
            substitution,
            steps
          };
        }
      }
    }

    steps.push(`โŒ No pattern matched for: ${importSpecifier}`);
    return {
      resolved: null,
      usedPattern: null,
      substitution: null,
      steps
    };
  }

  // ๐ŸŽฏ Match import specifier against pattern
  private matchPattern(pattern: string, importSpecifier: string): { groups: string[] } | null {
    // Convert TypeScript path pattern to regex
    const regexPattern = pattern
      .replace(/\*\*/g, '__DOUBLE_WILDCARD__')  // Temporarily replace **
      .replace(/\*/g, '([^/]*)')                // Single * matches within path segment
      .replace(/__DOUBLE_WILDCARD__/g, '(.*)')  // ** matches across path segments
      .replace(/\//g, '\\/');                   // Escape forward slashes

    const regex = new RegExp(`^${regexPattern}$`);
    const match = importSpecifier.match(regex);
    
    if (match) {
      console.log(`๐ŸŽฏ Pattern "${pattern}" matched "${importSpecifier}"`);
      console.log(`๐Ÿ“Š Captured groups:`, match.slice(1));
      return { groups: match.slice(1) };
    }
    
    return null;
  }

  // ๐Ÿ”„ Apply substitution with captured groups
  private applySubstitution(substitution: string, groups: string[]): string {
    let result = substitution;
    
    // Replace wildcards with captured groups
    groups.forEach((group, index) => {
      const singleWildcard = /\*/;
      const doubleWildcard = /\*\*/;
      
      if (doubleWildcard.test(result)) {
        result = result.replace(doubleWildcard, group);
      } else if (singleWildcard.test(result)) {
        result = result.replace(singleWildcard, group);
      }
    });
    
    return result;
  }

  // ๐Ÿ“ Resolve path relative to base URL
  private resolveRelativeToBase(path: string): string {
    if (path.startsWith('/')) {
      return path; // Absolute path
    }
    
    if (this.baseUrl === '.') {
      return path;
    }
    
    return `${this.baseUrl}/${path}`.replace(/\/+/g, '/'); // Normalize slashes
  }

  // โž• Add new path mapping
  addMapping(pattern: string, substitutions: string[]): void {
    this.mappings.set(pattern, substitutions);
    this.clearCache(); // Clear cache when mappings change
    
    console.log(`โž• Added mapping: ${pattern} -> [${substitutions.join(', ')}]`);
  }

  // ๐Ÿ—‘๏ธ Remove path mapping
  removeMapping(pattern: string): boolean {
    const removed = this.mappings.delete(pattern);
    if (removed) {
      this.clearCache();
      console.log(`๐Ÿ—‘๏ธ Removed mapping: ${pattern}`);
    }
    return removed;
  }

  // ๐Ÿ”„ Update existing mapping
  updateMapping(pattern: string, substitutions: string[]): boolean {
    if (this.mappings.has(pattern)) {
      this.mappings.set(pattern, substitutions);
      this.clearCache();
      console.log(`๐Ÿ”„ Updated mapping: ${pattern} -> [${substitutions.join(', ')}]`);
      return true;
    }
    return false;
  }

  // ๐Ÿงน Clear resolution cache
  clearCache(): void {
    this.resolutionCache.clear();
    console.log('๐Ÿงน Resolution cache cleared');
  }

  // ๐Ÿ“Š Get mapping statistics
  getStatistics(): {
    totalMappings: number;
    cacheSize: number;
    mostUsedPatterns: Array<{ pattern: string; usage: number }>;
    resolutionSuccess: number;
  } {
    // Simple statistics (in real implementation, track usage)
    return {
      totalMappings: this.mappings.size,
      cacheSize: this.resolutionCache.size,
      mostUsedPatterns: Array.from(this.mappings.keys()).map(pattern => ({
        pattern,
        usage: Math.floor(Math.random() * 100) // Mock usage data
      })),
      resolutionSuccess: 0.95 // Mock success rate
    };
  }

  // ๐Ÿ” Validate path mapping configuration
  validateConfiguration(): {
    isValid: boolean;
    issues: string[];
    warnings: string[];
    suggestions: string[];
  } {
    const issues: string[] = [];
    const warnings: string[] = [];
    const suggestions: string[] = [];

    // โœ… Check for baseUrl requirement
    if (this.mappings.size > 0 && !this.baseUrl) {
      issues.push('baseUrl is required when using path mappings');
    }

    // โœ… Check for overlapping patterns
    const patterns = Array.from(this.mappings.keys());
    for (let i = 0; i < patterns.length; i++) {
      for (let j = i + 1; j < patterns.length; j++) {
        if (this.patternsOverlap(patterns[i], patterns[j])) {
          warnings.push(`Patterns may overlap: "${patterns[i]}" and "${patterns[j]}"`);
        }
      }
    }

    // โœ… Check for performance issues
    if (this.mappings.size > 50) {
      warnings.push('Large number of path mappings may impact compilation performance');
      suggestions.push('Consider consolidating similar patterns');
    }

    // โœ… Check for common anti-patterns
    for (const pattern of patterns) {
      if (pattern.includes('node_modules')) {
        warnings.push(`Mapping to node_modules (${pattern}) may cause issues`);
      }
      
      if (pattern.split('*').length > 3) {
        warnings.push(`Complex pattern with multiple wildcards: ${pattern}`);
        suggestions.push('Simplify pattern for better maintainability');
      }
    }

    return {
      isValid: issues.length === 0,
      issues,
      warnings,
      suggestions
    };
  }

  // ๐Ÿ” Check if patterns overlap
  private patternsOverlap(pattern1: string, pattern2: string): boolean {
    // Simplified overlap detection
    const p1Base = pattern1.replace(/\*/g, '');
    const p2Base = pattern2.replace(/\*/g, '');
    
    return p1Base.startsWith(p2Base) || p2Base.startsWith(p1Base);
  }

  // ๐Ÿ“‹ List all configured mappings
  listMappings(): Array<{ pattern: string; substitutions: string[] }> {
    return Array.from(this.mappings.entries()).map(([pattern, substitutions]) => ({
      pattern,
      substitutions
    }));
  }

  // ๐ŸŽฏ Find best matching pattern for a given import
  findBestMatch(importSpecifier: string): {
    pattern: string;
    substitutions: string[];
    specificity: number;
  } | null {
    let bestMatch: { pattern: string; substitutions: string[]; specificity: number } | null = null;
    
    for (const [pattern, substitutions] of this.mappings) {
      if (this.matchPattern(pattern, importSpecifier)) {
        const specificity = this.calculateSpecificity(pattern);
        
        if (!bestMatch || specificity > bestMatch.specificity) {
          bestMatch = { pattern, substitutions, specificity };
        }
      }
    }
    
    return bestMatch;
  }

  // ๐Ÿ“Š Calculate pattern specificity (more specific patterns win)
  private calculateSpecificity(pattern: string): number {
    let specificity = 0;
    
    // More characters = more specific
    specificity += pattern.length;
    
    // Fewer wildcards = more specific
    const wildcardCount = (pattern.match(/\*/g) || []).length;
    specificity -= wildcardCount * 10;
    
    // Exact matches are most specific
    if (!pattern.includes('*')) {
      specificity += 1000;
    }
    
    return specificity;
  }
}

// ๐ŸŽฎ Advanced usage examples
const pathMappingDemo = async (): Promise<void> => {
  console.log('๐Ÿ—บ๏ธ Path Mapping Demonstration');
  
  // ๐Ÿ—๏ธ Initialize path mapping manager
  const pathMapper = new PathMappingManager({
    baseUrl: './src',
    paths: {
      '@/*': ['*'],
      '@components/*': ['components/*'],
      '@services/*': ['services/*'],
      '@utils/*': ['utils/*'],
      '@types/*': ['../types/*'],
      '@api': ['services/api/index'],
      'react-*': ['../node_modules/@types/react-*'],
      '@env': ['config/environments/production']
    }
  });

  // ๐Ÿ” Test various import resolutions
  const testImports = [
    '@/components/Button',
    '@components/ui/Modal',
    '@services/user/UserService',
    '@utils/validation/emailValidator',
    '@types/User',
    '@api',
    'react-dom',
    '@env'
  ];

  console.log('\n๐Ÿ“‹ Testing Import Resolutions:');
  for (const importSpec of testImports) {
    const result = pathMapper.resolveImport(importSpec);
    console.log(`\n๐Ÿ” Import: ${importSpec}`);
    console.log(`   โœ… Resolved: ${result.resolved}`);
    console.log(`   ๐ŸŽฏ Pattern: ${result.usedPattern}`);
    console.log(`   ๐Ÿ”„ Substitution: ${result.substitution}`);
  }

  // ๐Ÿ“Š Display statistics
  console.log('\n๐Ÿ“Š Path Mapping Statistics:');
  const stats = pathMapper.getStatistics();
  console.log(`   Total mappings: ${stats.totalMappings}`);
  console.log(`   Cache size: ${stats.cacheSize}`);
  console.log(`   Success rate: ${(stats.resolutionSuccess * 100).toFixed(1)}%`);

  // โœ… Validate configuration
  console.log('\nโœ… Configuration Validation:');
  const validation = pathMapper.validateConfiguration();
  console.log(`   Valid: ${validation.isValid}`);
  console.log(`   Issues: ${validation.issues.length}`);
  console.log(`   Warnings: ${validation.warnings.length}`);
  
  validation.warnings.forEach(warning => {
    console.log(`   โš ๏ธ ${warning}`);
  });
  
  validation.suggestions.forEach(suggestion => {
    console.log(`   ๐Ÿ’ก ${suggestion}`);
  });
};

๐Ÿ’ก Advanced Path Mapping Patterns

Letโ€™s explore sophisticated path mapping patterns for different project architectures:

// ๐ŸŒŸ Advanced path mapping configurations for different project types

// ๐Ÿข Enterprise monorepo configuration
const enterpriseMonorepoConfig = {
  compilerOptions: {
    baseUrl: "./",
    paths: {
      // ๐Ÿ“ฆ Package-based aliases
      "@core/*": ["packages/core/src/*"],
      "@ui/*": ["packages/ui/src/*"],
      "@api/*": ["packages/api/src/*"],
      "@shared/*": ["packages/shared/src/*"],
      "@utils/*": ["packages/utils/src/*"],
      
      // ๐ŸŽฏ App-specific aliases
      "@admin/*": ["apps/admin/src/*"],
      "@client/*": ["apps/client/src/*"],
      "@mobile/*": ["apps/mobile/src/*"],
      
      // ๐Ÿ”ง Development and testing
      "@test/*": ["test/*"],
      "@fixtures/*": ["test/fixtures/*"],
      "@mocks/*": ["test/mocks/*"],
      
      // ๐Ÿ“š Documentation and examples
      "@docs/*": ["docs/*"],
      "@examples/*": ["examples/*"],
      
      // ๐ŸŒ Environment configurations
      "@env": ["config/environment"],
      "@config/*": ["config/*"],
      
      // ๐ŸŽจ Assets and resources
      "@assets/*": ["assets/*"],
      "@public/*": ["public/*"],
      
      // ๐Ÿ”— Internal package references
      "core": ["packages/core/src/index"],
      "ui": ["packages/ui/src/index"],
      "api": ["packages/api/src/index"]
    },
    rootDirs: [
      "./packages/core/src",
      "./packages/ui/src",
      "./packages/api/src",
      "./packages/shared/src",
      "./apps/admin/src",
      "./apps/client/src"
    ]
  }
};

// โš›๏ธ React application configuration
const reactAppConfig = {
  compilerOptions: {
    baseUrl: "./src",
    paths: {
      // ๐ŸŽฏ Feature-based organization
      "@/*": ["*"],
      "@components/*": ["components/*"],
      "@pages/*": ["pages/*"],
      "@layouts/*": ["layouts/*"],
      "@features/*": ["features/*"],
      
      // ๐ŸŽฃ React-specific patterns
      "@hooks/*": ["hooks/*"],
      "@contexts/*": ["contexts/*"],
      "@providers/*": ["providers/*"],
      "@hoc/*": ["components/hoc/*"],
      
      // ๐Ÿ› ๏ธ Services and utilities
      "@services/*": ["services/*"],
      "@utils/*": ["utils/*"],
      "@api/*": ["services/api/*"],
      "@helpers/*": ["utils/helpers/*"],
      
      // ๐ŸŽจ Styling and assets
      "@styles/*": ["styles/*"],
      "@assets/*": ["assets/*"],
      "@images/*": ["assets/images/*"],
      "@icons/*": ["assets/icons/*"],
      "@fonts/*": ["assets/fonts/*"],
      
      // ๐Ÿงช Testing
      "@test-utils/*": ["../test/utils/*"],
      "@test-components/*": ["../test/components/*"],
      
      // ๐Ÿ“‹ Types and constants
      "@types/*": ["types/*"],
      "@constants/*": ["constants/*"],
      "@enums/*": ["enums/*"],
      
      // ๐ŸŽฏ Specific service aliases
      "api": ["services/api/index"],
      "auth": ["services/auth/index"],
      "storage": ["services/storage/index"],
      "analytics": ["services/analytics/index"]
    }
  }
};

// ๐Ÿš€ Full-stack application configuration
const fullStackConfig = {
  compilerOptions: {
    baseUrl: "./",
    paths: {
      // ๐ŸŽฏ Frontend aliases
      "@client/*": ["client/src/*"],
      "@client-components/*": ["client/src/components/*"],
      "@client-pages/*": ["client/src/pages/*"],
      "@client-utils/*": ["client/src/utils/*"],
      
      // ๐Ÿ› ๏ธ Backend aliases
      "@server/*": ["server/src/*"],
      "@controllers/*": ["server/src/controllers/*"],
      "@models/*": ["server/src/models/*"],
      "@services/*": ["server/src/services/*"],
      "@middleware/*": ["server/src/middleware/*"],
      "@routes/*": ["server/src/routes/*"],
      
      // ๐Ÿ“Š Database and ORM
      "@db/*": ["server/src/database/*"],
      "@entities/*": ["server/src/entities/*"],
      "@migrations/*": ["server/src/migrations/*"],
      "@seeds/*": ["server/src/seeds/*"],
      
      // ๐Ÿ” Authentication and security
      "@auth/*": ["server/src/auth/*"],
      "@guards/*": ["server/src/guards/*"],
      "@decorators/*": ["server/src/decorators/*"],
      
      // ๐ŸŒ Shared code
      "@shared/*": ["shared/*"],
      "@shared-types/*": ["shared/types/*"],
      "@shared-utils/*": ["shared/utils/*"],
      "@shared-constants/*": ["shared/constants/*"],
      
      // ๐Ÿงช Testing
      "@test/*": ["test/*"],
      "@client-tests/*": ["client/test/*"],
      "@server-tests/*": ["server/test/*"],
      
      // ๐Ÿ“‹ Configuration
      "@config/*": ["config/*"],
      "@env": ["config/environment"]
    },
    rootDirs: [
      "./client/src",
      "./server/src",
      "./shared"
    ]
  }
};

// ๐ŸŽจ Design system and component library configuration
const designSystemConfig = {
  compilerOptions: {
    baseUrl: "./src",
    paths: {
      // ๐ŸŽจ Component organization
      "@/*": ["*"],
      "@atoms/*": ["components/atoms/*"],
      "@molecules/*": ["components/molecules/*"],
      "@organisms/*": ["components/organisms/*"],
      "@templates/*": ["components/templates/*"],
      "@pages/*": ["components/pages/*"],
      
      // ๐ŸŽฏ Component utilities
      "@component-utils/*": ["utils/components/*"],
      "@component-types/*": ["types/components/*"],
      "@component-hooks/*": ["hooks/components/*"],
      
      // ๐ŸŽจ Design tokens and theming
      "@tokens/*": ["design-tokens/*"],
      "@themes/*": ["themes/*"],
      "@styles/*": ["styles/*"],
      "@mixins/*": ["styles/mixins/*"],
      "@variables/*": ["styles/variables/*"],
      
      // ๐Ÿ”ง System utilities
      "@system/*": ["system/*"],
      "@foundations/*": ["foundations/*"],
      "@patterns/*": ["patterns/*"],
      
      // ๐Ÿ“š Documentation and examples
      "@stories/*": ["../stories/*"],
      "@examples/*": ["../examples/*"],
      "@docs/*": ["../docs/*"],
      
      // ๐Ÿงช Testing and development
      "@test-utils/*": ["../test/utils/*"],
      "@visual-tests/*": ["../test/visual/*"],
      "@a11y-tests/*": ["../test/accessibility/*"],
      
      // ๐Ÿ“ฆ Build and distribution
      "@build/*": ["../build/*"],
      "@dist/*": ["../dist/*"]
    }
  }
};

// ๐Ÿ”ง Advanced path mapping utilities
class AdvancedPathMappingUtilities {
  // ๐ŸŽฏ Generate path mapping for micro-frontend architecture
  static generateMicrofrontendPaths(
    microfrontends: Array<{ name: string; port: number; routes: string[] }>,
    sharedLibraries: string[]
  ): Record<string, string[]> {
    const paths: Record<string, string[]> = {};
    
    // ๐Ÿ—๏ธ Microfrontend aliases
    microfrontends.forEach(mf => {
      paths[`@${mf.name}/*`] = [`microfrontends/${mf.name}/src/*`];
      paths[`@${mf.name}`] = [`microfrontends/${mf.name}/src/index`];
      
      // Route-specific aliases
      mf.routes.forEach(route => {
        paths[`@${mf.name}/${route}`] = [`microfrontends/${mf.name}/src/routes/${route}`];
      });
    });
    
    // ๐Ÿ“š Shared library aliases
    sharedLibraries.forEach(lib => {
      paths[`@shared/${lib}`] = [`shared/${lib}/src/index`];
      paths[`@shared/${lib}/*`] = [`shared/${lib}/src/*`];
    });
    
    // ๐ŸŒ Shell application aliases
    paths['@shell/*'] = ['shell/src/*'];
    paths['@shell'] = ['shell/src/index'];
    
    return paths;
  }

  // ๐ŸŽฏ Generate environment-specific path mappings
  static generateEnvironmentPaths(
    environments: string[],
    configs: Record<string, string[]>
  ): Record<string, string[]> {
    const paths: Record<string, string[]> = {};
    
    environments.forEach(env => {
      // Environment-specific configuration
      paths[`@config/${env}`] = [`config/environments/${env}`];
      paths[`@env/${env}/*`] = [`config/environments/${env}/*`];
      
      // Environment-specific services
      if (configs.services) {
        paths[`@services/${env}/*`] = [`services/${env}/*`];
      }
      
      // Environment-specific assets
      if (configs.assets) {
        paths[`@assets/${env}/*`] = [`assets/${env}/*`];
      }
    });
    
    // Default environment (usually production)
    const defaultEnv = environments[0] || 'production';
    paths['@env'] = [`config/environments/${defaultEnv}`];
    
    return paths;
  }

  // ๐ŸŽฏ Generate feature-based path mappings
  static generateFeaturePaths(
    features: Array<{
      name: string;
      hasComponents?: boolean;
      hasServices?: boolean;
      hasPages?: boolean;
      hasUtils?: boolean;
    }>
  ): Record<string, string[]> {
    const paths: Record<string, string[]> = {};
    
    features.forEach(feature => {
      const featureName = feature.name;
      
      // Main feature alias
      paths[`@${featureName}`] = [`features/${featureName}/index`];
      paths[`@${featureName}/*`] = [`features/${featureName}/*`];
      
      // Feature-specific sub-paths
      if (feature.hasComponents) {
        paths[`@${featureName}/components/*`] = [`features/${featureName}/components/*`];
      }
      
      if (feature.hasServices) {
        paths[`@${featureName}/services/*`] = [`features/${featureName}/services/*`];
      }
      
      if (feature.hasPages) {
        paths[`@${featureName}/pages/*`] = [`features/${featureName}/pages/*`];
      }
      
      if (feature.hasUtils) {
        paths[`@${featureName}/utils/*`] = [`features/${featureName}/utils/*`];
      }
    });
    
    return paths;
  }

  // ๐Ÿ”ง Optimize path mappings for performance
  static optimizePathMappings(
    paths: Record<string, string[]>
  ): {
    optimized: Record<string, string[]>;
    optimizations: string[];
    warnings: string[];
  } {
    const optimized: Record<string, string[]> = {};
    const optimizations: string[] = [];
    const warnings: string[] = [];
    
    Object.entries(paths).forEach(([pattern, substitutions]) => {
      // โœ… Remove duplicate substitutions
      const uniqueSubstitutions = [...new Set(substitutions)];
      if (uniqueSubstitutions.length !== substitutions.length) {
        optimizations.push(`Removed duplicate substitutions from ${pattern}`);
      }
      
      // โœ… Sort substitutions by specificity (most specific first)
      const sortedSubstitutions = uniqueSubstitutions.sort((a, b) => {
        return b.length - a.length; // Longer paths are usually more specific
      });
      
      // โš ๏ธ Check for potentially problematic patterns
      if (pattern.includes('**')) {
        warnings.push(`Double wildcard in pattern ${pattern} may impact performance`);
      }
      
      if (substitutions.length > 5) {
        warnings.push(`Many substitutions for pattern ${pattern} may slow resolution`);
      }
      
      optimized[pattern] = sortedSubstitutions;
    });
    
    return { optimized, optimizations, warnings };
  }

  // ๐Ÿ” Analyze path mapping usage and suggest improvements
  static analyzePathMappingUsage(
    paths: Record<string, string[]>,
    actualImports: string[]
  ): {
    utilization: Record<string, number>;
    suggestions: string[];
    unusedPatterns: string[];
  } {
    const utilization: Record<string, number> = {};
    const suggestions: string[] = [];
    const unusedPatterns: string[] = [];
    
    // Initialize utilization counters
    Object.keys(paths).forEach(pattern => {
      utilization[pattern] = 0;
    });
    
    // Count usage of each pattern
    actualImports.forEach(importSpec => {
      Object.keys(paths).forEach(pattern => {
        const regex = new RegExp(pattern.replace(/\*/g, '.*'));
        if (regex.test(importSpec)) {
          utilization[pattern]++;
        }
      });
    });
    
    // Identify unused patterns
    Object.entries(utilization).forEach(([pattern, count]) => {
      if (count === 0) {
        unusedPatterns.push(pattern);
      }
    });
    
    // Generate suggestions
    if (unusedPatterns.length > 0) {
      suggestions.push(`Consider removing ${unusedPatterns.length} unused path mappings`);
    }
    
    const highUsagePatterns = Object.entries(utilization)
      .filter(([, count]) => count > actualImports.length * 0.1)
      .map(([pattern]) => pattern);
    
    if (highUsagePatterns.length > 0) {
      suggestions.push(`High-usage patterns should be optimized: ${highUsagePatterns.join(', ')}`);
    }
    
    return { utilization, suggestions, unusedPatterns };
  }

  // ๐ŸŽฏ Generate IDE-friendly path mappings
  static generateIDEConfig(
    tsconfigPaths: Record<string, string[]>,
    baseUrl: string
  ): {
    vscode: any;
    webstorm: any;
    eslint: any;
  } {
    // ๐ŸŽฏ VS Code configuration
    const vscode = {
      "typescript.preferences.useAliasesForRenames": false,
      "typescript.preferences.includePackageJsonAutoImports": "auto",
      "typescript.suggest.autoImports": true,
      "typescript.suggest.paths": true
    };
    
    // ๐ŸŽฏ WebStorm configuration
    const webstorm = {
      "mark_directory_as": "sources_root",
      "resource_patterns": Object.keys(tsconfigPaths).map(pattern => 
        pattern.replace(/\*/g, '*')
      )
    };
    
    // ๐ŸŽฏ ESLint configuration
    const eslint = {
      "settings": {
        "import/resolver": {
          "typescript": {
            "alwaysTryTypes": true,
            "project": "./tsconfig.json"
          }
        }
      },
      "rules": {
        "import/no-unresolved": ["error", {
          "ignore": Object.keys(tsconfigPaths).map(pattern => 
            `^${pattern.replace(/\*/g, '.*')}$`
          )
        }]
      }
    };
    
    return { vscode, webstorm, eslint };
  }
}

// ๐ŸŽฎ Advanced usage demonstration
const advancedPathMappingDemo = async (): Promise<void> => {
  console.log('๐Ÿš€ Advanced Path Mapping Demonstration');
  
  // ๐Ÿ—๏ธ Generate microfrontend paths
  const microfrontendPaths = AdvancedPathMappingUtilities.generateMicrofrontendPaths(
    [
      { name: 'dashboard', port: 3001, routes: ['overview', 'analytics'] },
      { name: 'users', port: 3002, routes: ['list', 'profile', 'settings'] },
      { name: 'billing', port: 3003, routes: ['invoices', 'payments'] }
    ],
    ['ui-components', 'utils', 'api-client']
  );
  
  console.log('๐Ÿ—๏ธ Microfrontend paths:', microfrontendPaths);
  
  // ๐ŸŒ Generate environment paths
  const envPaths = AdvancedPathMappingUtilities.generateEnvironmentPaths(
    ['production', 'staging', 'development'],
    { services: ['api'], assets: ['images'] }
  );
  
  console.log('๐ŸŒ Environment paths:', envPaths);
  
  // ๐ŸŽฏ Generate feature paths
  const featurePaths = AdvancedPathMappingUtilities.generateFeaturePaths([
    { name: 'auth', hasComponents: true, hasServices: true, hasUtils: true },
    { name: 'dashboard', hasComponents: true, hasPages: true },
    { name: 'profile', hasComponents: true, hasServices: true, hasPages: true }
  ]);
  
  console.log('๐ŸŽฏ Feature paths:', featurePaths);
  
  // ๐Ÿ”ง Optimize combined paths
  const allPaths = { ...microfrontendPaths, ...envPaths, ...featurePaths };
  const optimization = AdvancedPathMappingUtilities.optimizePathMappings(allPaths);
  
  console.log('๐Ÿ”ง Optimization results:');
  console.log('   Optimizations:', optimization.optimizations);
  console.log('   Warnings:', optimization.warnings);
  
  // ๐Ÿ“Š Analyze usage
  const mockImports = [
    '@dashboard/components/Chart',
    '@shared/ui-components/Button',
    '@auth/services/AuthService',
    '@env/api',
    '@users/pages/UserList'
  ];
  
  const analysis = AdvancedPathMappingUtilities.analyzePathMappingUsage(
    optimization.optimized,
    mockImports
  );
  
  console.log('๐Ÿ“Š Usage analysis:');
  console.log('   Utilization:', analysis.utilization);
  console.log('   Suggestions:', analysis.suggestions);
  console.log('   Unused patterns:', analysis.unusedPatterns);
};

๐ŸŽ‰ Conclusion

Congratulations! Youโ€™ve mastered the art of TypeScript path mapping! ๐Ÿ—บ๏ธ

๐ŸŽฏ What Youโ€™ve Learned

  • ๐Ÿ—บ๏ธ Path Mapping Fundamentals: Understanding baseUrl, paths, and resolution patterns
  • ๐ŸŽฏ Advanced Patterns: Complex mapping strategies for different project architectures
  • ๐Ÿ”ง Configuration Optimization: Performance tuning and best practices
  • ๐Ÿ—๏ธ Project Architecture: Organizing large codebases with intelligent path mapping
  • ๐Ÿ› ๏ธ Development Workflow: IDE integration and tooling optimization

๐Ÿš€ Key Benefits

  • ๐Ÿ“ Clean Imports: Replace messy relative paths with intuitive aliases
  • ๐Ÿ”ง Maintainable Code: Easy refactoring without breaking imports
  • โšก Better DX: Improved developer experience with autocomplete and navigation
  • ๐Ÿ“ฆ Scalable Architecture: Organize complex projects with logical path structure
  • ๐ŸŽฏ Team Productivity: Consistent import patterns across the entire team

๐Ÿ”ฅ Best Practices Recap

  1. ๐Ÿ“ Logical Organization: Use meaningful aliases that reflect your project structure
  2. ๐ŸŽฏ Consistent Patterns: Establish team conventions for path mapping usage
  3. โšก Performance Awareness: Avoid overly complex patterns that slow compilation
  4. ๐Ÿ”ง IDE Integration: Configure your development environment for optimal support
  5. ๐Ÿ“Š Regular Review: Analyze and optimize path mappings as your project evolves

Youโ€™re now equipped to create sophisticated, maintainable import systems that will make your TypeScript projects a joy to work with! ๐ŸŒŸ

Happy coding, and may your imports always be clean and your paths well-mapped! ๐Ÿ—บ๏ธโœจ