TypeScript
Beginner
1 min read
Path Aliases and Barrel Files
Example
// tsconfig.json path aliases
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["src/app/*"],
"@shared/*": ["src/shared/*"],
"@features/*": ["src/features/*"],
"@utils": ["src/utils/index.ts"]
}
}
}
// src/shared/models/user.model.ts
export interface User { id: number; name: string; email: string }
// src/shared/models/post.model.ts
export interface Post { id: number; title: string; authorId: number }
// src/shared/models/index.ts — barrel file
export type { User } from "./user.model";
export type { Post } from "./post.model";
// src/shared/index.ts — top-level barrel
export * from "./models";
export * from "./utils";
export * from "./constants";
// Consuming code — clean imports
import type { User, Post } from "@shared";
// Without alias (messy):
// import type { User } from "../../../shared/models/user.model";
// With alias (clean):
// import type { User } from "@shared";
function getUserPost(user: User, post: Post): string {
return `${user.name} wrote "${post.title}"`;
}
export { getUserPost };
Related Resources
TypeScript Reference
Complete tag & property list
TypeScript How-To Guides
Step-by-step practical guides
TypeScript Exercises
Practice what you've learned
More in TypeScript