typeof का Guard
In this page:
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
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
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
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
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
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);
- गलत string के खिलाफ compare करना, जैसे
typeof value === "String"या"integer", जब typeof सिर्फ lowercase names return करता है जैसे"string"और"number"। - एक plain object detect करने के लिए
typeof x === "object"उपयोग करना, जब यहnullऔर arrays के लिए भी true होता है। - यह उम्मीद करना कि
typeofclasses या interfaces के बीच फर्क बताएगा, जब हर non-primitive object सिर्फ"object"report करता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: