← Back to TypeScript Course | Chapter 2: Basic Types | Lesson 7 of 11

unknown का Type

unknown type किसी भी type की value रख सकता है, लेकिन TypeScript आपको इस पर ज़्यादातर operations perform करने से पहले value जांचने के लिए require करती है। यह unknown को any से safer बनाता है जब ऐसे data के साथ काम कर रहे हों जिसका type पता नहीं।
Syntax
typescript
let variable: unknown = value;
if (typeof variable === "type") {
  // variable is narrowed here
}

Basic Unknown Type

unknown से typed एक variable किसी भी type की value रख सकता है, any जैसा ही, लेकिन TypeScript आपको इस पर operations perform करने से मना करती है जब तक आप prove न करें कि यह असल में क्या है।

यह genuinely uncertain data के लिए any का type-safe counterpart बनाता है।

उदाहरण: Basic Unknown Type

typescript
let value: unknown = "hello";
// console.log(value.length); // rejected: must narrow the type first
console.log(value);

Unknown Values जांचना

एक typeof check आपको type-specific operations perform करने से पहले एक unknown value को एक specific type, जैसे string या number, तक narrow करने देता है। एक बार narrowed branch के अंदर, TypeScript automatically value को उस specific type के रूप में treat करती है।

उदाहरण: Checking Unknown Values

typescript
let value: unknown = "hello";
if (typeof value === "string") {
  console.log(value.length); // safe: TypeScript knows it's a string here
}

Objects के साथ Unknown

जब एक unknown value एक object हो सकती है, इसकी structure verify करने की ज़रूरत है, उदाहरण के लिए एक particular property exist करने की जांच करके, इससे पहले इसकी कोई properties safely access की जा सकें।

यह check skip करना और सीधे एक property access करना एक compile-time error है।

उदाहरण: Unknown with Objects

typescript
let value: unknown = { name: "Ira" };
if (typeof value === "object" && value !== null && "name" in value) {
  console.log((value as { name: string }).name);
}

Unknown और Type Guards

Type guards एक unknown value को कुछ ज़्यादा specific और usable तक narrow करने का general mechanism हैं। Common guards में primitives के लिए typeof, class instances के लिए instanceof, और plain objects के लिए manual property checks शामिल हैं।

उदाहरण: Unknown and Type Guards

typescript
function printLength(value: unknown): void {
  if (typeof value === "string") {
    console.log(value.length);
  } else if (Array.isArray(value)) {
    console.log(value.length);
  }
}
printLength("hello");

Unknown बनाम Any

any और unknown दोनों technically किसी भी type की value store कर सकते हैं, लेकिन unknown किसी भी useful चीज़ करने से पहले validation demand करता है, जबकि any सब कुछ unchecked गुज़र जाने देता है।

एक external या untrusted source से आने वाले data के लिए, unknown almost always safer default है।

उदाहरण: Unknown Compared with Any

typescript
let unsafeValue: any = "42";
let safeValue: unknown = "42";
console.log(unsafeValue.toUpperCase()); // allowed, may crash at runtime
// console.log(safeValue.toUpperCase()); // rejected until narrowed
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. value.toUpperCase() जैसा एक method एक unknown value पर directly call करना, जो एक error है जब तक आप इसे narrow न करें।
  2. unknown को any जैसे उपयोग करना और typeof से check करने के बजाय as string से cast करना।
  3. यह भूल जाना कि narrowing सिर्फ if branch के अंदर apply होती है, इसलिए check के बाद value उपयोग करना फिर से fail होता है।
🔒

Chapter Quiz — Complete all 11 topics to unlock

0/11 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.