TypeScript का Compiler (tsc)
In this page:
tsc filename.ts
tsc --watch
tsc --outDir dist filename.ts
tsc क्या करता है
tsc command TypeScript package द्वारा provide किया गया main command-line compiler है। यह आपकी .ts source files पढ़ता है, आपके code के against full type checking perform करता है, और JavaScript हर जगह run करने के लिए तैयार plain JavaScript output emit कर सकता है।
उदाहरण: What tsc Does
let total: number = 5 + 10;
console.log(total);
// tsc type-checks this file, then emits plain JavaScript
एक Single File Compile करना
आप tsc को सिर्फ वह file compile करने के लिए एक specific file name दे सकते हैं, उदाहरण के लिए tsc app.ts। By default यह source के ठीक बगल में एक matching app.js file बनाता है, TypeScript syntax को equivalent JavaScript में translate करते हुए।
उदाहरण: Compiling a Single File
// Run in a terminal (not executable here):
// tsc app.ts
// creates app.js next to app.ts
console.log("Compiled with: tsc app.ts");
Type Checking
Compiler जांचता है कि आपका code आपके पूरे program में declared type rules follow करता है या नहीं, सिर्फ syntax errors नहीं। Compiler errors ऐसी problems सामने लाती हैं जैसे generated program execute होने से पहले एक function को गलत argument types के साथ call करना।
उदाहरण: Type Checking
function multiply(a: number, b: number): number {
return a * b;
}
// multiply(2, "3"); // compiler error: wrong argument type
console.log(multiply(2, 3));
Compiler Options उपयोग करना
Compiler कई options support करता है यह control करने के लिए कि types कितनी strictly check होते हैं और JavaScript output कैसे generate होता है, जैसे target language version या module format।
ये options directly command line पर pass किए जा सकते हैं या, ज़्यादा commonly, एक tsconfig.json file में centrally store किए जा सकते हैं।
उदाहरण: Using Compiler Options
// Run in a terminal (not executable here):
// tsc app.ts --target ES2015 --module commonjs
console.log("Compiler options control target JS version and module format");
एक Project Compile करना
बड़े projects के लिए, tsc को बिना किसी file name के run करना इसे एक tsconfig.json file ढूंढने और इसकी settings के according पूरा project compile करने के लिए कहता है। एक project single file से बढ़ने के बाद यह standard workflow है।
उदाहरण: Compiling a Project
// Run in a terminal (not executable here):
// tsc
// compiles the whole project using tsconfig.json
console.log("Running tsc with no filename uses tsconfig.json");
tsc app.tsrun करना और फिरapp.tsexecute करना generatedapp.jsके बजाय।- यह भूल जाना कि
tsctype errors report करता है लेकिन फिर भीapp.jsलिखता है, इसलिए errors वाला एक project ऐसा लग सकता है जैसे ठीक से build हुआ। - एक
tsconfig.jsonवाले project मेंtscको एक file name pass करना और पता चलना कि config ignore हो गया।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: