Path Aliases की जानकारी
In this page:
{
"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
// 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
// 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
// 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
// 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
// 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");
tsconfig.jsonमेंpathsजोड़ना और Node से runtime पर alias resolve होने की उम्मीद करना, जबtscइन्हें rewrite नहीं करता और एक bundler या runtime tool को भी इन्हें जानना चाहिए।"@utils/*": "src/utils/*"को paths के एक array के बजाय एक string के साथ लिखना।- Alias और target दोनों पर
/*भूल जाना, इसलिए@utils/helpermatch नहीं करता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: