← Back to TypeScript Course | Chapter 3: Variables and Functions | Lesson 3 of 10

Type का Inference

Type inference TypeScript को automatically किसी value के initial value से उसका type determine करने देता है। इसका मतलब है कि आपको अक्सर explicitly एक type annotation लिखने की ज़रूरत नहीं होती।
Syntax
typescript
let variable = value; // type inferred from value

Basic Inference

जब एक variable एक value से initialize होता है, TypeScript उस value से उसका type automatically infer कर सकता है बिना किसी explicit annotation की ज़रूरत के। let age = 25 लिखना age को inferred type number देता है, कोई extra syntax चाहिए बिना।

उदाहरण: Basic Inference

typescript
let age = 25; // inferred as number, no annotation needed
console.log(typeof age, age);

Inference with const

Constants को भी जो भी value वे assign होते हैं उससे एक inferred type मिलता है, और चूंकि const values को कभी reassign नहीं किया जा सकता, TypeScript कभी-कभी एक similarly initialized let से ज़्यादा specific literal type infer कर सकता है।

उदाहरण: Inference with const

typescript
const name = "Fixed"; // inferred as the literal type "Fixed", not just string
console.log(name);

Inference in Arrays

TypeScript एक array के element type को सीधे उसके initializer के अंदर रखे values से infer कर सकता है, तो const nums = [1, 2, 3] automatically एक number[] के रूप में समझा जाता है, कोई annotation ज़रूरत नहीं।

Mixed-type array literals उन types के एक union के रूप में infer होते हैं।

उदाहरण: Inference in Arrays

typescript
const nums = [1, 2, 3]; // inferred as number[]
console.log(nums);

Inference in Expressions

Types भी expressions और operations के result से infer होते हैं, जैसे एक function का return type उसके return statement से जो भी असल में produce होता है उससे infer होना।

यह आपको simple functions पर return types annotate करने से बचने देता है जबकि TypeScript उन्हें फिर भी पूरी तरह checked रखता है।

उदाहरण: Inference in Expressions

typescript
function add(a: number, b: number) {
  return a + b; // return type inferred as number
}
console.log(add(2, 3));

Inference and Explicit Types

Type inference unnecessary explicit annotations की ज़रूरत घटाता है, code को कम cluttered रखते हुए, जबकि explicit annotations तब भी उपयोगी हैं जब आप एक intended type clearly document करना चाहें या एक variable को जानबूझकर TypeScript के otherwise infer किए जाने वाले से narrower type तक restrict करना चाहें।

उदाहरण: Inference and Explicit Types

typescript
let count = 5; // inferred, no clutter
let id: number = 5; // explicit, documents intent
console.log(count, id);
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 x; बिना किसी value के declare करना और एक type infer होने की उम्मीद करना, जब यह any बन जाता है।
  2. यह उम्मीद करना कि const name = "Fixed" सिर्फ string होगा, जब यह literal type "Fixed" के रूप में inferred है।
  3. एक empty array जैसे const items = [] initialize करना और एक specific element type की उम्मीद करना।

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.