TypeScript 5 के Updates
In this page:
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)
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)
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)
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)
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
type Value = "a" | "b" | "c";
function check(v: Value) {
switch (true) {
case v === "a":
return "is a";
default:
return "other";
}
}
console.log(check("a"));
- TypeScript 5.0 से पहले वाले versions पर
consttype parameters उपयोग करना, जहां यह एक syntax error है। Symbol.disposesupport न करने वाले target/library के साथ, या[Symbol.dispose]method न रखने वाली value परusingdeclarations उपयोग करना।- यह उम्मीद करना कि
const Tinferred type को readonly बना देगा जब constraintunknown[]जैसा एक mutable array हो।
- Mapped, conditional, और template literal types मौजूदा types से नए types बनाते हैं।
- Custom type guards, assertion functions, control flow analysis, exhaustiveness checking, और
satisfiescompiler को narrow और validate करने में मदद करते हैं। Partial,Pick,Omit,Record, औरReturnTypeजैसे Built-in utility types types को फिर से लिखे बिना transform करते हैं।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates