TExcelTAD
Home › TypeScript › TS 5 Updates

TypeScript 5 Updates

TypeScript ships a new minor version roughly every three months. Since version 5.0 (March 2023) the releases have focused on smarter inference, stricter checks for common mistakes and support for new JavaScript features. The playgrounds on this site use TypeScript 5.9, so every feature below works here.

Highlights by version

Version Notable additions
5.0 standard ECMAScript decorators, const type parameters, moduleResolution: "bundler", all enums are union enums
5.1 easier implicit returns for undefined-returning functions, unrelated getter/setter types
5.2 using declarations (explicit resource management), decorator metadata
5.3 import attributes (with { type: "json" }), narrowing in switch (true)
5.4 NoInfer<T>, narrowing preserved in closures after the last assignment
5.5 inferred type predicates, regular-expression syntax checking, isolatedDeclarations
5.6 errors for always-truthy / always-nullish checks, iterator helper types
5.7 checks for never-initialised variables, --rewriteRelativeImportExtensions
5.8 --erasableSyntaxOnly (for Node's built-in type stripping), granular return checks
5.9 import defer, a leaner tsc --init, expandable hovers in editors

Let's try a few of the most useful ones.

const type parameters (5.0)

Normally, passing ["a", "b"] to a generic function infers string[]. Adding const before the type parameter asks TypeScript to infer the narrowest literal type, as if the caller had written as const:

ts · try it yourself
function routes<const T extends readonly string[]>(paths: T): T {
  return paths;
}

function routesLoose<T extends readonly string[]>(paths: T): T {
  return paths;
}

const strict = routes(["/home", "/about"]);       // readonly ["/home", "/about"]
const loose = routesLoose(["/home", "/about"]);   // string[]

const first: "/home" = strict[0];
const alsoFirst: "/home" = loose[0];
main.ts(13,7): error TS2322: Type 'string' is not assignable to type '"/home"'.

Only the loose version lost the literal types.

Inferred type predicates (5.5)

Before 5.5, filtering out undefined from an array left the undefined in the type, and you had to write a type guard by hand. Now TypeScript works out that the arrow function is a type predicate:

ts · try it yourself
const answers = ["yes", undefined, "no", undefined, "maybe"];

const given = answers.filter((a) => a !== undefined);   // string[] since 5.5

console.log(given.map((a) => a.toUpperCase()));
[ 'YES', 'NO', 'MAYBE' ]

In TypeScript 5.4 the same code failed with 'a' is possibly 'undefined'.

NoInfer<T> (5.4)

When a type parameter appears in several places, TypeScript infers it from all of them. Wrapping one occurrence in NoInfer says "don't use this spot for inference, just check against it":

ts · try it yourself
function pickColor<C extends string>(options: C[], fallback: NoInfer<C>): C {
  return options[0] ?? fallback;
}

console.log(pickColor(["red", "green"], "green"));
pickColor(["red", "green"], "purple");
main.ts(6,29): error TS2345: Argument of type '"purple"' is not assignable to parameter of type '"red" | "green"'.

Without NoInfer, C would quietly widen to "red" | "green" | "purple" and the typo would be accepted.

Catching bugs the compiler used to miss (5.5–5.7)

Newer versions report several classic mistakes:

ts · try it yourself
const datePattern = /(\d{4})-(\d{2}/;

function check(input: string) {
  if (/^\d+$/) {
    return "number";
  }
  return "text";
}

function report() {
  let total: number;
  const format = () => total.toFixed(2);
  return format();
}
main.ts(1,36): error TS1005: ')' expected.
main.ts(4,7): error TS2872: This kind of expression is always truthy.
main.ts(12,24): error TS2454: Variable 'total' is used before being assigned.
  1. 5.5 checks regular-expression syntax — the second group is missing its ).
  2. 5.6 notices that a regex literal in an if is always truthy — the author forgot .test(input).
  3. 5.7 sees that total is never assigned at all, so reading it inside the nested arrow function is always a bug.

Standard decorators and using

TypeScript 5.0 implemented the official ECMAScript decorators proposal; you no longer need the experimentalDecorators flag. They get their own lesson later in this course. TypeScript 5.2 added using, which automatically cleans up a resource when the variable goes out of scope:

ts
function openLog() {
  return {
    write(line: string) { console.log(line); },
    [Symbol.dispose]() { console.log("log closed"); },
  };
}

{
  using log = openLog();
  log.write("hello");
} // prints "hello", then "log closed"

using requires the esnext.disposable lib and a runtime that provides Symbol.dispose, so it is shown here as a plain snippet.

Tip: To see what changed in any version, read the official "Announcing TypeScript 5.x" posts on the TypeScript blog. Upgrading is usually painless, but new checks may flag real bugs in old code — which is a good thing.

Test yourself

What does function f<const T>(x: T) change?

  1. T can't be reassigned
  2. T is inferred as the narrowest literal type, like as const
  3. The function becomes pure
  4. x becomes read-only at runtime

Since TS 5.5, what is the type of ["a", undefined].filter(x => x !== undefined)?

  1. (string | undefined)[]
  2. string[]
  3. any[]
  4. never[]

What is NoInfer<T> for?

  1. Disabling type checking for T
  2. Preventing a position from contributing to T's inference
  3. Making T optional
  4. Converting T to unknown