Member-only story

TypeScript@5.4 latest news

Beck Moulton
11 min readMay 3, 2024

--

npm install -D typescript@rc

Here is a quick list of what’s new in TypeScript 5.4!

  1. Retain closure after last assignment Narrow scope
  2. Utility NoInfer type
  3. Object.groupBy and Map.groupBy
  4. Support require () callers — moduleResolution bundler and — module preserve
  5. Check import properties and assertions
  6. Quick fix for adding missing parameters
  7. Upcoming 5.5 deprecation, significant behavioral changes

Retain closure after last assignment Narrow scope

TypeScript can usually identify the more specific type of a variable based on the checks you may perform. This process is called narrowing.

function uppercaseStrings(x: string | number) {
if (typeof x === "string") {
// TypeScript knows 'x' is a 'string' here.
return x.toUpperCase();
}
}

A common pain point is that these reduced types are not always retained in function closures.

function getUrls(url: string | URL, names: string[]) {
if (typeof url === "string") {
url = new URL(url);
} return names.map(name => {
url.searchParams.set("name", name)
// ~~~~~~~~~~~~
// error!
// Property 'searchParams' does not exist on type 'string | URL'. return url.toString();
});
}

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet