TypeScript ek open-source, object-oriented, compiled programming language hai jo JavaScript ka superset hai. Matlab, JavaScript ke saare features TypeScript mein bhi hote hain, lekin TypeScript mein kuch extra features hote hain, jaise static typing, classes, interfaces, aur modules, jo JavaScript mein nahi hote.
TypeScript ko Microsoft ne develop kiya hai aur iska main goal JavaScript code ko zyada reliable aur maintainable banana hai. TypeScript ka code ultimately JavaScript mein compile hota hai, jisse wo har modern web browser ya JavaScript engine mein run kar sake.
TypeScript Ke Features:
Static Typing:
- TypeScript ka sabse important feature static typing hai. Matlab aap variables, function parameters, aur return types ko explicitly define kar sakte hain. Isse compile time par errors jaldi detect ho jaate hain, jo runtime errors ko reduce karta hai.
- Example:
- let name: string = 'John'; // string type variablelet age: number = 25; // number type variable
Type Inference:
- Agar aap variable ki type nahi specify karte hain, toh TypeScript automatic inference kar leta hai aur variable ki type ko samajh leta hai.
- Example:
- let name = 'John'; // TypeScript automatically infers type 'string'
Classes and Interfaces:
- TypeScript object-oriented programming ko support karta hai. Aap classes, interfaces, inheritance, aur polymorphism ka use kar sakte hain.
- Example of Class and Interface:
- interface Person {name: string;age: number;}class Employee implements Person {constructor(public name: string, public age: number, public salary: number) {}getDetails() {return `${this.name} is ${this.age} years old and earns ${this.salary}`;}}const emp = new Employee('John', 30, 50000);console.log(emp.getDetails()); // Output: John is 30 years old and earns 50000
Modules:
- TypeScript mein aap apne code ko modules mein organize kar sakte hain. Modules ko import aur export kiya ja sakta hai.
- Example of Module:
- // math.tsexport function add(a: number, b: number): number {return a + b;}// app.tsimport { add } from './math';console.log(add(5, 3)); // Output: 8
Generics:
- TypeScript mein generics ka use kar ke aap reusable code likh sakte hain, jisme types ko generic placeholders ke roop mein define kiya jata hai.
- Example:
- function identity<T>(arg: T): T {return arg;}let result = identity<string>('Hello');console.log(result); // Output: Hello
Type Aliases and Unions:
- TypeScript mein aap type aliases create kar sakte hain aur union types ka use kar ke multiple types ko combine kar sakte hain.
- Example:
- type StringOrNumber = string | number;function printValue(value: StringOrNumber) {console.log(value);}printValue('Hello');printValue(100);
Decorators:
- TypeScript mein decorators ka support hota hai, jo aapke classes, methods, aur properties ko dynamically modify karne ki facility dete hain. Yeh advanced feature hai jo mostly Angular framework mein use hota hai.
Error Detection:
- TypeScript static type checking ke through code likhne ke dauraan hi errors detect kar leta hai. Isse run-time errors reduce hote hain.
- Example:
- let num: number = 5;num = 'Hello'; // Error: Type 'string' is not assignable to type 'number'.
TypeScript vs JavaScript:
Feature | TypeScript | JavaScript |
---|---|---|
Typing | Static typing (Type annotations) | Dynamic typing (No type annotations) |
Compilation | Needs to be compiled into JavaScript | Directly interpreted by browsers |
Classes/Interfaces | Supports object-oriented features (classes, interfaces, etc.) | Limited support for classes (ES6 onwards) |
Error Detection | Compile-time error detection | Run-time error detection |
Code Organization | Supports modules and namespaces | Modules were added in ES6 |
Learning Curve | Slightly steeper due to additional features | Easier to start with |
TypeScript ka Faydah:
- Better Code Quality: Static typing aur compile-time error detection se code quality improve hoti hai.
- Easy Refactoring: Types ki wajah se code refactoring asaan hota hai. Aap easily samajh sakte hain ki kis type ka data kis jagah pe use ho raha hai.
- Maintainability: TypeScript ka code zyada maintainable hota hai, especially jab project bada ho, kyunki aap types define kar sakte hain aur errors easily detect kar sakte hain.
- Improved Tooling: IDEs aur editors TypeScript ke types ko samajhte hain, jo autocompletion, error detection, aur refactoring ko improve karte hain.
- Supports Modern JavaScript Features: TypeScript aapko modern JavaScript features ka support deta hai, chahe wo classes, async/await, ya ES6+ features ho, jo purane browsers mein nahi hote.
TypeScript Example:
In this example, Person
type ko define kiya gaya hai, aur person
variable ko us type ke according assign kiya gaya hai. Agar aap person
object ko galat type ka data assign karte hain, toh TypeScript compile time par error dega.
Conclusion:
TypeScript ek powerful language hai jo JavaScript ke shortcomings ko fix karta hai aur aapko ek strongly-typed, object-oriented approach provide karta hai. Yeh large-scale applications ke liye ideal hai, jahan type safety aur maintainability important hote hain. TypeScript ko adopt karne se aapko better tooling, improved error checking, aur maintainability milti hai, jo JavaScript mein nahi hoti.