Member-only story
TypeScript@5.4 latest news
11 min readMay 3, 2024
npm install -D typescript@rc
Here is a quick list of what’s new in TypeScript 5.4!
- Retain closure after last assignment Narrow scope
- Utility NoInfer type
- Object.groupBy and Map.groupBy
- Support require () callers — moduleResolution bundler and — module preserve
- Check import properties and assertions
- Quick fix for adding missing parameters
- 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();
});
}