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:

  1. 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 variable
          let age: number = 25;       // number type variable
  2. 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'
  3. 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
  4. 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.ts
          export function add(a: number, b: number): number {
              return a + b;
          }

          // app.ts
          import { add } from './math';

          console.log(add(5, 3));  // Output: 8
  5. 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
  6. 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);
  7. 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.
  8. 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:

FeatureTypeScriptJavaScript
TypingStatic typing (Type annotations)Dynamic typing (No type annotations)
CompilationNeeds to be compiled into JavaScriptDirectly interpreted by browsers
Classes/InterfacesSupports object-oriented features (classes, interfaces, etc.)Limited support for classes (ES6 onwards)
Error DetectionCompile-time error detectionRun-time error detection
Code OrganizationSupports modules and namespacesModules were added in ES6
Learning CurveSlightly steeper due to additional featuresEasier to start with

TypeScript ka Faydah:

  1. Better Code Quality: Static typing aur compile-time error detection se code quality improve hoti hai.
  2. 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.
  3. 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.
  4. Improved Tooling: IDEs aur editors TypeScript ke types ko samajhte hain, jo autocompletion, error detection, aur refactoring ko improve karte hain.
  5. 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:


    // Defining a type for a person
    type Person = {
    name: string;
    age: number;
    };

    const person: Person = {
    name: 'John',
    age: 25,
    };

    console.log(person.name);  // Output: John
    console.log(person.age);   // Output: 25


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.

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions