← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 1 of 20

TypeScript के Advanced Types

TypeScript की advanced type features आपको दूसरे types से types compose, narrow, और generate करने देती हैं, basic annotations से आगे जाकर shapes model करते हुए जो runtime data के आधार पर बदलते हैं या जो दूसरे types से programmatically बनाए जाते हैं।
Syntax
typescript
type Name<T> = /* type expression using T */;

Intersection Types Overview

एक intersection type, & से लिखा जाता है, कई types को एक में combine करता है जिसे एक साथ हर combined type satisfy करना चाहिए, तो A & B के रूप में typed एक value को दोनों A और B की सारी properties चाहिए बजाय सिर्फ किसी एक से match करने पर valid होने के।

उदाहरण: Intersection Types Overview

typescript
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const user: Person = { name: "Ravi", age: 25 };
console.log(user);

Discriminated Unions

एक discriminated union object types का एक union है जो एक common literal property share करते हैं, जिसे अक्सर kind या status कहा जाता है, जिसे TypeScript एक if या switch check के अंदर union को एक specific member तक narrow करने के लिए उपयोग करता है, उस member की unique properties तक safe access देते हुए।

उदाहरण: Discriminated Unions

typescript
type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; side: number };

function area(shape: Shape): number {
  if (shape.kind === "circle") return Math.PI * shape.radius ** 2;
  return shape.side ** 2;
}
console.log(area({ kind: "square", side: 4 }));

Template Literal Types

Template literal types backticks के अंदर दूसरे literal या union types interpolate करके नए string literal types बनाते हैं, बिल्कुल JavaScript template strings जैसे, तो Direction और Side जैसे दो unions combine करना हर possible dash-joined combination को एक type के रूप में produce करता है।

उदाहरण: Template Literal Types

typescript
type Direction = "top" | "bottom";
type Side = "left" | "right";
type Corner = `${Direction}-${Side}`;
const c: Corner = "top-left";
console.log(c);

Recursive Types

एक recursive type अपनी ही definition में खुद को refer करता है, जो TypeScript को JSON values या nested arrays जैसी deeply nested या self-similar structures describe करने देता है, जहां किसी भी level पर एक value खुद उसी shape की और values रख सकता है।

उदाहरण: Recursive Types

typescript
type Json = string | number | boolean | Json[] | { [key: string]: Json };
const data: Json = { name: "Ravi", tags: ["a", "b"], nested: { active: true } };
console.log(data);

Combining Advanced Type Features

Advanced type features शायद ही अलग-अलग उपयोग होते हैं: real-world type definitions अक्सर APIs, state machines, और दूसरी structures को precisely model करने के लिए intersections, discriminated unions, template literals, और mapped types को एक साथ combine करती हैं।

उदाहरण: Combining Advanced Type Features

typescript
type Event =
  | { type: "click"; target: `#${string}` }
  | { type: "keydown"; key: string };

function handle(e: Event) {
  if (e.type === "click") console.log("Clicked", e.target);
  else console.log("Key", e.key);
}
handle({ type: "click", target: "#button" });
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. string & number जैसे incompatible primitives intersect करना, जो never तक reduce हो जाता है तो कोई value इसे satisfy नहीं कर सकता।
  2. Literal kind property भूल जाना (या इसे plain string टाइप करना), तो union discriminated नहीं है और narrowing काम नहीं करती।
  3. एक ऐसा recursive type लिखना जिसमें एक cycle हो जो कभी खत्म न हो, जैसे type A = A | string style aliases, बजाय recursion को एक object या array के अंदर रखने के।

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.