# स्ट्रिक्ट मोड और कंपाइलर त्रुटियाँ — TypeScript Fundamentals

Source: https://www.skillbyai.com/hi/typescript/ts-strict-mode

> Strict mode चालू करें, null और undefined को स्पष्ट रूप से संभालें, और fix जल्दी खोजने के लिए TypeScript compiler errors पढ़ें।

## strict फ़्लैग

`tsconfig.json` में `"strict": true`, कई जाँचों — `strictNullChecks`, `noImplicitAny`, और अन्य — को एक स्विच में बाँध देता है। नए प्रोजेक्ट को पहले दिन से इसे चालू रखना चाहिए।

## strictNullChecks की पकड़

स्ट्रिक्ट नल जाँच में, `null`/`undefined` हर जगह चुपचाप स्वीकार नहीं होते — आपको इन्हें स्पष्ट रूप से हैंडल करना होगा।

```typescript
function getLength(s: string | null): number {
  return s.length;
  // Error: 's' is possibly 'null'.
}

function getLengthSafe(s: string | null): number {
  return s ? s.length : 0; // narrowed, OK
}
```

## कंपाइलर त्रुटियाँ पढ़ना

TS त्रुटि संदेश असंगत टाइप्स के ठीक-ठीक नाम बताते हैं ("Type 'X' is not assignable to type 'Y'") — पहले दोनों टाइप नाम पढ़ें; वे आमतौर पर सीधे समाधान की ओर इशारा करते हैं।

**Quiz:** tsconfig.json में `"strict": true` क्या करता है?

- [ ] एक अतिरिक्त जाँच सक्षम करता है
- [x] कई स्ट्रिक्ट टाइप-चेकिंग विकल्पों को एक साथ बाँधता है
- [ ] केवल आउटपुट फ़ाइल के नामों को प्रभावित करता है
- [ ] Test files में type checking बंद करता है

*Answer:* कई स्ट्रिक्ट टाइप-चेकिंग विकल्पों को एक साथ बाँधता है. strict एक शॉर्टहैंड है जो strictNullChecks, noImplicitAny, और कई अन्य जाँचों को एक साथ चालू करता है।

## अंतिम क्विज़ 1/8

अंतिम क्विज़

**Quiz:** `tsconfig.json` का कौन-सा option पूरे strict checks चालू करता है?

- [ ] noEmit
- [x] strict
- [ ] target
- [ ] outDir

*Answer:* strict. `strict: true` strictNullChecks, noImplicitAny आदि को एक साथ चालू करता है।

## अंतिम क्विज़ 2/8

अंतिम क्विज़

**Quiz:** अप्रत्याशित data के लिए `any` की जगह कौन-सा type चुनें?

- [x] unknown
- [ ] object
- [ ] Function
- [ ] never

*Answer:* unknown. `unknown` उपयोग से पहले narrowing अनिवार्य करता है।

## अंतिम क्विज़ 3/8

अंतिम क्विज़

**Quiz:** `[string, number]` जैसा tuple क्या लागू करता है?

- [ ] Any length of mixed values
- [x] A fixed length with a type at each position
- [ ] Only string values
- [ ] Readonly access

*Answer:* A fixed length with a type at each position. Tuples लंबाई और हर position का type दोनों तय करते हैं।

## अंतिम क्विज़ 4/8

अंतिम क्विज़

**Quiz:** Shared field से union को TypeScript कैसे narrow करे?

- [x] Use a discriminated union with a literal `kind`
- [ ] Cast with `as any`
- [ ] Use `Partial<T>`
- [ ] Declare the field optional

*Answer:* Use a discriminated union with a literal `kind`. Literal discriminant से `switch` हर variant को narrow करता है।

## अंतिम क्विज़ 5/8

अंतिम क्विज़

**Quiz:** `Partial<User>` क्या बनाता है?

- [x] A type with every property optional
- [ ] A type with every property readonly
- [ ] A type with only required keys
- [ ] A union of keys

*Answer:* A type with every property optional. `Partial<T>` सभी properties को optional बनाता है।

## अंतिम क्विज़ 6/8

अंतिम क्विज़

**Quiz:** `T extends { length: number }` क्या करता है?

- [x] Constrains T to types that have a length property
- [ ] Makes T a number
- [ ] Adds a length field at runtime
- [ ] Disables inference

*Answer:* Constrains T to types that have a length property. Constraints generic को सीमित करते हैं पर उसे reusable रखते हैं।

## अंतिम क्विज़ 7/8

अंतिम क्विज़

**Quiz:** `import type { Point }` आउटपुट JavaScript के साथ क्या करता है?

- [ ] Adds a runtime import
- [x] Is erased at compile time
- [ ] Bundles the type
- [ ] Creates a class

*Answer:* Is erased at compile time. Type-only imports emitted JS से हटा दिए जाते हैं।

## अंतिम क्विज़ 8/8

अंतिम क्विज़

**Quiz:** `Promise<User>` पर `await` कौन-सा type देता है?

- [ ] Promise<User>
- [x] User
- [ ] unknown
- [ ] void

*Answer:* User. `await` Promise को उसके resolved type में unwrap करता है।
