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

TypeScript 5 Updates

TypeScript 5.x brought a steady stream of smaller, practical features on top of the type system basics, from smarter generic inference to new runtime-facing syntax like using declarations.

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)

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 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)

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 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)

typescript
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)

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 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

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"));

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.