Initialize the Path Alias Resolver
Loads path mappings from tsconfig.json and prepares for resolution.
ts-morph Project instance with tsconfig loaded
Optionaloptions: PathAliasResolverOptionsOptional configuration
Add a custom path alias at runtime
Allows extending the alias set beyond tsconfig.json for dynamic scenarios.
Path alias configuration to add
Clear the resolution cache
Useful when aliases are modified at runtime.
Get the count of registered aliases
Number of aliases
Get all registered path aliases
Array of all path alias configurations
Get cache statistics
Cache size information
Number of cached alias resolutions.
Preprocess file content to replace path aliases in import statements
Scans file content for import/export statements and replaces alias-based paths with relative paths. This prevents parser errors on valid code.
Original file content with path aliases
Path to the file being preprocessed (for relative path calculation)
Preprocessed content with resolved import paths
const original = `
import { SharedModule } from '@shared/modules';
import { UserService } from '@domain/user/services/user.service';
`;
const preprocessed = resolver.preprocessFileContent(
original,
'src/app/user/user.component.ts'
);
// Result:
// import { SharedModule } from '../../shared/modules';
// import { UserService } from '../../../domain/user/services/user.service';
Resolve a single import path using registered aliases
Converts alias-based imports to relative paths based on the target file location.
Import path to resolve (e.g., "@shared/modules")
File path where the import is used (for relative path calculation)
Resolved import path, or original if no alias matches
Path Alias Resolver
Resolves TypeScript path aliases from tsconfig.json to prevent parser errors on valid import statements. Commonly used for custom path mappings like:
Use Cases
Custom Path Aliases
External Decorator Projects
Monorepo Packages
Architecture
Example