← Back to TypeScript Course | Chapter 1: Introduction to TypeScript | Lesson 5 of 7

TypeScript का Compiler (tsc)

TypeScript compiler, जिसे आमतौर पर tsc कहा जाता है, TypeScript source code जांचता है और इसे JavaScript में transform करता है। यह JavaScript version, output directory, strictness, और कई दूसरे compiler behaviors control करने के लिए configuration options भी उपयोग कर सकता है।
Syntax
typescript
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

typescript
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

typescript
// 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

typescript
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

typescript
// 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

typescript
// 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");
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. tsc app.ts run करना और फिर app.ts execute करना generated app.js के बजाय।
  2. यह भूल जाना कि tsc type errors report करता है लेकिन फिर भी app.js लिखता है, इसलिए errors वाला एक project ऐसा लग सकता है जैसे ठीक से build हुआ।
  3. एक 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:

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.