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

Template Literal Types की जानकारी

Template literal types existing string literal types को combine करके नए string literal types बनाते हैं। वे structured strings describe करने के लिए उपयोगी हैं।
Syntax
typescript
type Name = `${string}-${string}`;
type Union = `${"a" | "b"}_${"x" | "y"}`;

Basic Template Literal Type

एक template literal type backticks और placeholders उपयोग करता है, बिल्कुल एक JavaScript template string जैसा, existing string literal types को नए, ज़्यादा specific string literal types में combine करने के लिए।

उदाहरण: Basic Template Literal Type

typescript
type Greeting = `Hello, ${string}!`;
const g: Greeting = "Hello, Ravi!";
console.log(g);

Combining Unions

Template literal types कई unions combine कर सकते हैं उनके members का हर valid combination produce करने के लिए, जो छोटे pieces से related string values का एक exhaustive set generate करने के लिए उपयोगी है।

उदाहरण: Combining Unions

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

Event Names

Template literal types property names से predictable, structured event names बना सकते हैं — उदाहरण के लिए एक property click को अपने आप एक event type onClick में बदलना।

उदाहरण: Event Names

typescript
type Prop = "click" | "hover";
type EventName = `on${Capitalize<Prop>}`;
const e: EventName = "onClick";
console.log(e);

Intrinsic String Utilities

Template literal types built-in string utility types जैसे Uppercase, Lowercase, और Capitalize के साथ मिलकर काम करते हैं वे जो string literal types produce करते हैं उनकी casing transform करने के लिए।

उदाहरण: Intrinsic String Utilities

typescript
type Loud = Uppercase<"hello">;
type Quiet = Lowercase<"WORLD">;
const loud: Loud = "HELLO";
const quiet: Quiet = "world";
console.log(loud, quiet);

Practical Template Literal Types

Template literal types APIs, event systems, CSS-like values, और किसी भी और जगह के लिए उपयोगी हैं जहां string values एक predictable, structured pattern follow करती हैं जो type level पर enforce करने लायक है।

उदाहरण: Practical Template Literal Types

typescript
type HttpMethod = "GET" | "POST";
type Endpoint = "users" | "orders";
type Route = `${HttpMethod} /${Endpoint}`;
const r: Route = "POST /users";
console.log(r);
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. Backticks के बजाय single या double quotes उपयोग करना, जैसे "Hello, ${string}", जो सिर्फ एक string literal type है।
  2. बड़े unions combine करना और huge results से surprised होना, क्योंकि members की संख्या multiply होती है।
  3. एक value assign करना जो pattern से match नहीं करता, जैसे "Hi, Ravi!" को Hello, ${string}! में।

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.