पाठ 14 / 28
लिटरल टाइप्स
Literal types से values को सटीक strings या numbers तक सीमित करें, और const बनाम let inference समझें।
लेखक: Rahul Agarwal · 15 साल से वेब डेवलपर · अपडेट: 1 अक्टू॰ 2026
बिल्कुल तय अनुमत मान
लिटरल यूनियन किसी मान को स्ट्रिंग या संख्याओं के तय सेट तक सीमित करता है — एक हल्के एनम जैसा।
type Direction = "up" | "down" | "left" | "right";
function move(dir: Direction) { /* ... */ }
move("up"); // OK
move("diagonal");
// Error: Argument of type '"diagonal"' is not assignable to type 'Direction'.const और लिटरल विडनिंग
let x = "up" string के रूप में इन्फर होता है, पर const x = "up" लिटरल "up" के रूप में — const दोबारा असाइन नहीं हो सकता, इसलिए TS संकरा टाइप रखता है।