← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 20 of 20

TypeScript 5 के Updates

TypeScript 5.x type system basics के ऊपर छोटे, practical features की एक steady stream लाया, smarter generic inference से लेकर using declarations जैसे नए runtime-facing syntax तक।
Syntax
typescript
function functionName<const T extends readonly unknown[]>(param: T): T {
    return param;   // T is inferred as the narrowest literal type
}

Const Type Parameters (TS 5.0)

TypeScript 5.0 ने type parameters पर const modifier introduce किया, तो <const T extends ...> लिखने से compiler किसी argument के लिए सबसे narrow possible literal type infer करता है, arrays को उनके base element type तक widen करने के बजाय।

उदाहरण: Const Type Parameters (TS 5.0)

typescript
function first<const T extends readonly unknown[]>(arr: T): T[0] {
  return arr[0];
}
const result = first(["a", "b", "c"]);
console.log(result);

Tuple and Array Spread Improvements (TS 5.0)

TypeScript 5.0 ने refine किया कि tuple types spread और labeled elements के साथ कैसे combine होते हैं, functions को [...T, ...U] जैसे variadic tuples accept और return करने देते हुए, documentation और editor IntelliSense के लिए named, individually typed positions बनाए रखते हुए।

उदाहरण: Tuple and Array Spread Improvements (TS 5.0)

typescript
function combine<T extends unknown[], U extends unknown[]>(a: [...T], b: [...U]): [...T, ...U] {
  return [...a, ...b];
}
console.log(combine([1, 2], ["a", "b"]));

Return Type Improvements (TS 5.1)

TypeScript 5.1 ने एक rule relax किया ताकि बिना किसी return statement वाले function को explicitly undefined return करने के लिए typed किया जा सके, और undefined-returning type को assigned functions को type checker संतुष्ट करने के लिए अब explicit return statement की ज़रूरत नहीं।

उदाहरण: Return Type Improvements (TS 5.1)

typescript
function noop(): undefined {
  // no explicit return needed in TS 5.1+
}
console.log(noop());

using Declarations (TS 5.2)

TypeScript 5.2 ने explicit resource management के लिए using declarations जोड़े: using से declared एक value का Symbol.dispose method अपने आप call होता है जब यह scope से बाहर जाता है, जो files, connections, या दूसरे resources बंद करने के लिए उपयोगी है।

उदाहरण: using Declarations (TS 5.2)

typescript
class Resource {
  [Symbol.dispose]() {
    console.log("Resource disposed");
  }
}
function run() {
  using res = new Resource();
  console.log("Using resource");
}
run();

TS 5.3 and 5.4 Highlights

TypeScript 5.3 ने JSON जैसे non-JavaScript module imports को type करने के लिए import attributes जोड़े, plus switch(true) statements के अंदर narrowing, जबकि 5.4 ने narrowing check के बाद बनाए closures के अंदर narrowed types preserve किए और NoInfer utility type जोड़ा।

उदाहरण: TS 5.3 and 5.4 Highlights

typescript
type Value = "a" | "b" | "c";
function check(v: Value) {
  switch (true) {
    case v === "a":
      return "is a";
    default:
      return "other";
  }
}
console.log(check("a"));
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. TypeScript 5.0 से पहले वाले versions पर const type parameters उपयोग करना, जहां यह एक syntax error है।
  2. Symbol.dispose support न करने वाले target/library के साथ, या [Symbol.dispose] method न रखने वाली value पर using declarations उपयोग करना।
  3. यह उम्मीद करना कि const T inferred type को readonly बना देगा जब constraint unknown[] जैसा एक mutable array हो।
चैप्टर सारांश
  • Mapped, conditional, और template literal types मौजूदा types से नए types बनाते हैं।
  • Custom type guards, assertion functions, control flow analysis, exhaustiveness checking, और satisfies compiler को narrow और validate करने में मदद करते हैं।
  • Partial, Pick, Omit, Record, और ReturnType जैसे Built-in utility types types को फिर से लिखे बिना transform करते हैं।

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.