TypeScript 5 Updates
In this page:
Const Type Parameters (TS 5.0)
TypeScript 5.0 introduced the const modifier on type parameters, so writing <const T extends ...> makes the compiler infer the narrowest possible literal type for an argument instead of widening arrays to their base element type.
Example: 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 refined how tuple types combine with spread and labeled elements, letting functions accept and return variadic tuples such as [...T, ...U] while keeping named, individually typed positions for documentation and editor IntelliSense.
Example: 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 relaxed a rule so that a function with no return statement can be typed to return undefined explicitly, and functions assigned to an undefined-returning type no longer need an explicit return statement to satisfy the type checker.
Example: 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 added using declarations for explicit resource management: a value declared with using has its Symbol.dispose method called automatically when it goes out of scope, which is useful for closing files, connections, or other resources.
Example: 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 added import attributes for typing non-JavaScript module imports like JSON, plus narrowing inside switch(true) statements, while 5.4 preserved narrowed types inside closures created after the narrowing check and added the NoInfer utility type.
Example: 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"));
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