← Back to TypeScript Course | Chapter 17: TypeScript Configuration | Lesson 4 of 7

Path Aliases की जानकारी

Path aliases अक्सर उपयोग होने वाले project paths को छोटे नाम देते हैं। TypeScript baseUrl और paths से इन aliases को define करता है, imports को पढ़ने और maintain करने में आसान बनाते हुए।
Syntax
typescript
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@alias/*": ["src/path/*"]
    }
  }
}

baseUrl

baseUrl वह base directory establish करता है जिसे TypeScript non-relative module names resolve करते समय उपयोग करता है, "utils/helpers" जैसे imports को "../../utils/helpers" चाहने के बजाय उस directory पर rooted lookups में बदलते हुए।

उदाहरण: baseUrl

typescript
// tsconfig.json
// { "compilerOptions": { "baseUrl": "./src" } }
// import { helper } from "utils/helper"; // resolved from src/
console.log("baseUrl roots non-relative imports at a chosen directory");

एक Path Alias Define करना

paths option एक alias pattern को एक या ज़्यादा असली project locations से map करता है, तो आप एक long relative path जो file move करने पर हर बार टूटता है उसके बजाय एक short, stable import path लिख सकते हैं।

उदाहरण: Defining a Path Alias

typescript
// tsconfig.json
// { "compilerOptions": { "baseUrl": ".", "paths": { "@utils/*": ["src/utils/*"] } } }
// import { helper } from "@utils/helper";
console.log("paths maps a short alias to a real project location");

Imports में Aliases उपयोग करना

एक बार configure होने पर, TypeScript aliased import paths को type checking और editor autocomplete दोनों के दौरान समझता है, तो compiler जहां तक मायने रखता है alias exactly एक असली module path की तरह behave करता है।

उदाहरण: Using Aliases in Imports

typescript
// import { formatDate } from "@utils/date";
console.log("Aliased imports work in both type checking and editor autocomplete");

Multiple Targets वाले Aliases

एक single alias कई target patterns की ओर point कर सकता है, और TypeScript हर configured substitution को order में तब तक try करता है जब तक एक resolve न हो जाए — उपयोगी जब एक symbol एक से ज़्यादा possible location में रह सकता हो।

उदाहरण: Aliases with Multiple Targets

typescript
// tsconfig.json
// { "compilerOptions": { "paths": { "@shared/*": ["packages/a/*", "packages/b/*"] } } }
console.log("A single alias can try multiple target locations in order");

Runtime Considerations

TypeScript का paths option सिर्फ यह affect करता है कि TypeScript खुद type checking के दौरान modules कैसे resolve करता है — यह असली runtime import paths rewrite नहीं करता, तो आपके bundler या runtime को भी match करने के लिए अपना alias configuration चाहिए।

उदाहरण: Runtime Considerations

typescript
// tsconfig.json's paths only affects TypeScript's own type checking --
// a bundler (webpack, Vite) needs its own matching alias config to run.
console.log("paths doesn't rewrite runtime import paths on its own");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. tsconfig.json में paths जोड़ना और Node से runtime पर alias resolve होने की उम्मीद करना, जब tsc इन्हें rewrite नहीं करता और एक bundler या runtime tool को भी इन्हें जानना चाहिए।
  2. "@utils/*": "src/utils/*" को paths के एक array के बजाय एक string के साथ लिखना।
  3. Alias और target दोनों पर /* भूल जाना, इसलिए @utils/helper match नहीं करता।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.