← Back to TypeScript Course | Chapter 5: Type Aliases and Union Types | Lesson 4 of 9

Literal के Types

Literal types एक variable को एक broader type की किसी भी value के बजाय exactly एक value contain करने देते हैं। वे strings, numbers, या booleans के precise sets बनाने के लिए उपयोगी हैं।
Syntax
typescript
let variable: "value1" | "value2" | "value3";
type Literal = 1 | 2 | 3;

String Literal Types

एक string literal type एक single, exact string value represent करता है, जैसे general type string के बजाय type admin। कई string literals को एक union के साथ combine करना, जैसे admin | editor | viewer, allowed choices का एक controlled, closed set बनाता है।

उदाहरण: String Literal Types

typescript
type Role = 'admin' | 'editor';
let role: Role = 'admin';
console.log(role);

Number Literal Types

Number literal types numbers के लिए उसी तरह काम करते हैं, एक value को किसी भी number के बजाय एक या ज़्यादा specific exact numbers तक restrict करते हैं।

वे खासकर तब handy हैं जब वाकई सिर्फ एक छोटा, fixed set ही valid options हो, जैसे HTTP status codes।

उदाहरण: Number Literal Types

typescript
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
let roll: DiceRoll = 4;
console.log(roll);

Boolean Literal Types

Boolean literal types एक value को exactly true या exactly false तक अपने distinct type के रूप में restrict कर सकते हैं, general boolean type के बजाय जो दोनों allow करता है।

यह day-to-day कम common है पर एक specific, fixed object state model करते समय उपयोगी हो सकता है।

उदाहरण: Boolean Literal Types

typescript
type AlwaysTrue = true;
let flag: AlwaysTrue = true;
console.log(flag);

Literal Types in Functions

Function parameters में literal types callers को एक specific, supported set से बाहर कोई भी value pass करने से रोकते हैं, typos और unsupported options को runtime के बजाय compile time पर पकड़ते हुए।

वे editors को valid values के लिए precise autocomplete suggestions देने के लिए भी enough information देते हैं।

उदाहरण: Literal Types in Functions

typescript
function setAlign(value: 'left' | 'center' | 'right') {
  console.log(`Aligned: ${value}`);
}
setAlign('center');

Literal Types with Type Aliases

एक literal union को एक type alias के साथ pair करना, जैसे type Theme = light | dark, उस allowed values के set को codebase में variables, functions, और object properties में reusable बनाता है।

यह application states और fixed configuration options represent करने के लिए एक common और effective pattern है।

उदाहरण: Literal Types with Type Aliases

typescript
type Theme = 'light' | 'dark';
let theme: Theme = 'dark';
console.log(theme);
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. let role = admin को let के साथ declare करना और literal type की उम्मीद करना, जब यह string तक widen हो जाता है।
  2. Union के बाहर एक value assign करना, जैसे guest को admin | editor में।
  3. DiceRoll जैसे एक literal के लिए गलत number उपयोग करना, जैसे 7, जो reject हो जाता है।
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

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.