TypeScript क्या है
In this page:
TypeScript को समझना
TypeScript JavaScript का एक superset है, यानी valid JavaScript code आमतौर पर एक TypeScript file में उपयोग किया जा सकता है। TypeScript type annotations, interfaces, और advanced type checking जैसी features add करती है।
उदाहरण: Understanding TypeScript
let age: number = 25;
let city = "Delhi"; // plain JS-style code still works in TypeScript
console.log(age, city);
Static Type Checking
TypeScript के मुख्य benefits में से एक static type checking है। Compiler यह detect कर सकता है कि program execute होने से पहले एक value declared type से match नहीं करती।
उदाहरण: Static Type Checking
let age: number = 25;
// age = "twenty five"; // compiler catches this mismatch before running
console.log(typeof age, age);
TypeScript और JavaScript
TypeScript JavaScript को एक पूरी तरह अलग runtime से replace करने के बजाय इसे extend करती है। TypeScript code उस JavaScript में transform होती है जो browsers, Node.js, और दूसरे JavaScript environments में run हो सकती है।
उदाहरण: TypeScript and JavaScript
let message: string = "Compiled to plain JavaScript";
console.log(message);
// tsc removes ": string" and outputs: console.log(message);
TypeScript के फायदे
TypeScript बेहतर editor support, साफ़ code, और earlier error detection देकर बड़े applications को maintain करना आसान बना सकती है। Types developers के लिए यह समझना भी आसान बनाते हैं कि functions और variables किस तरह की values expect करते हैं।
उदाहरण: Benefits of TypeScript
interface User {
id: number;
name: string;
}
const user: User = { id: 1, name: "Asha" };
console.log(user.id, user.name);
TypeScript कब उपयोग करें
TypeScript खास तौर पर medium और large applications के लिए useful है जहां कई files, developers, और data structures को साथ काम करना होता है। इसे छोटे projects के लिए भी उपयोग किया जा सकता है जब आपको stronger type checking और बेहतर development tools चाहिए हों।
उदाहरण: When to Use TypeScript
interface Order {
id: number;
total: number;
}
function printOrder(order: Order): void {
console.log(`Order ${order.id}: $${order.total}`);
}
printOrder({ id: 101, total: 49.99 });
- यह सोचना कि TypeScript सीधे browser या Node.js में run होती है, जबकि इसे पहले plain JavaScript में compile (या transform) किया जाना ज़रूरी है।
- यह मानना कि
let age: numberजैसी type annotations runtime पर मौजूद रहती हैं, जब वे erase हो जाती हैं और code run होने से पहले सिर्फ check होती हैं। - यह assume करना कि TypeScript JavaScript को एक नई language से replace कर देती है, जब यह एक superset है इसलिए आपको normal JavaScript अब भी जानना ज़रूरी है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: