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

typeof का Guard

एक typeof guard एक value का runtime type determine करने के लिए JavaScript के typeof operator का उपयोग करता है। TypeScript इस check को पहचानता है और appropriate branch के अंदर value को narrow करता है।
Syntax
typescript
if (typeof variable === "string") {
  // variable is string here
}

Strings Check करना

typeof operator एक string value पर apply होने पर literal string "string" return करता है, और specifically उस result के लिए check करना TypeScript के लिए एक union type को string तक narrow करने के लिए काफी है।

उस narrowed branch के अंदर, हर string method safely available हो जाता है।

उदाहरण: Checking Strings

typescript
function process(value: string | number) {
  if (typeof value === "string") {
    console.log(value.toUpperCase());
  }
}
process("hello");

Numbers Check करना

typeof number type की values के लिए "number" return करता है, और उस result पर guard करना TypeScript को number-specific operations और methods, जैसे toFixed, safely allow करने देता है, जो union के दूसरे members पर safe नहीं होते।

उदाहरण: Checking Numbers

typescript
function process(value: string | number) {
  if (typeof value === "number") {
    console.log(value.toFixed(2));
  }
}
process(3.14159);

Booleans Check करना

typeof true और false दोनों values के लिए "boolean" return करता है, जो इसे एक reliable guard बनाता है जब भी boolean values किसी union में mixed हों या किसी external source से genuinely unknown input process करते समय।

यह boolean handling को हर दूसरे possible type से cleanly separate करता है।

उदाहरण: Checking Booleans

typescript
function process(value: boolean | string) {
  if (typeof value === "boolean") {
    console.log(value ? "Yes" : "No");
  }
}
process(true);

Functions Check करना

typeof operator specifically उन values के लिए "function" return करता है जो actually call की जा सकती हैं। यह आपको एक unknown या union-typed value को invoke करने से पहले safely verify करने देता है कि वह callable है, एक ऐसी चीज़ call करने से होने वाला runtime TypeError avoid करते हुए जो function नहीं है।

उदाहरण: Checking Functions

typescript
function run(value: unknown) {
  if (typeof value === "function") {
    value();
  }
}
run(() => console.log("Called!"));

Multiple Types Check करना

Multiple typeof checks को साथ chain किया जा सकता है, typically else-if branches के साथ, एक function के अंदर कई possible primitive types handle करने के लिए। यह खासकर उपयोगी है जब एक function primitives के एक broad union को accept करने के लिए design किया गया हो, या एक unknown value भी, और हर case के लिए अपना behavior branch करने की ज़रूरत हो।

उदाहरण: Checking Multiple Types

typescript
function describe(value: string | number | boolean) {
  if (typeof value === "string") {
    console.log("String:", value);
  } else if (typeof value === "number") {
    console.log("Number:", value);
  } else if (typeof value === "boolean") {
    console.log("Boolean:", value);
  }
}
describe(42);
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 के खिलाफ compare करना, जैसे typeof value === "String" या "integer", जब typeof सिर्फ lowercase names return करता है जैसे "string" और "number"।
  2. एक plain object detect करने के लिए typeof x === "object" उपयोग करना, जब यह null और arrays के लिए भी true होता है।
  3. यह उम्मीद करना कि typeof classes या interfaces के बीच फर्क बताएगा, जब हर non-primitive object सिर्फ "object" report करता है।
🔒

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.