Announcing the Release Candidate (RC) of TypeScript 5.8
In this article, Daniel Rosenwasser details the new features and changes in the Release Candidate of TypeScript 5.8, aimed at improving type checking, module interoperability, build performance, and compatibility with modern JavaScript.
Announcing TypeScript 5.8 Release Candidate (RC)
By Daniel Rosenwasser and the TypeScript Team
Today, the TypeScript team is excited to announce the Release Candidate (RC) for TypeScript 5.8. This milestone brings several new features, improvements, and optimizations. Developers are encouraged to try out the RC using npm:
npm install -D typescript@rc
What’s New Since the Beta?
- Conditional Return Type Checking:
- TypeScript 5.8 improves checking of conditional return expressions within functions, enabling better error detection when return branches do not match the function’s declared return type. For example, returning a string where a
URL
object is expected now results in an error.
- TypeScript 5.8 improves checking of conditional return expressions within functions, enabling better error detection when return branches do not match the function’s declared return type. For example, returning a string where a
-
Example:
declare const untypedCache: Map<any, any>; function getUrlObject(urlString: string): URL { return untypedCache.has(urlString) ? untypedCache.get(urlString) : urlString; // Error: Type 'string' is not assignable to type 'URL'. }
The relevant pull request details this enhancement.
Enhanced Module Interoperability
- Support for
require()
of ECMAScript Modules in--module nodenext
:- Node.js 22 now allows CommonJS files to
require()
ESM files (except those with top-levelawait
). TypeScript 5.8 supports this if you use the--module nodenext
flag, making it easier for library authors to support ESM without dual-publishing. - More information on implementation.
- Node.js 22 now allows CommonJS files to
--module node18
Flag:
New Compiler Flags & Compatibility Options
--erasableSyntaxOnly
:- Node.js 23.6 unflags support for running TypeScript files directly, but only permits easily erasable syntax. This flag makes TypeScript error for any syntax that can’t be simply stripped out (e.g., namespaces with runtime code, parameter properties, enums,
import =
aliases). Combining this flag with--verbatimModuleSyntax
is recommended for stricter enforcement. - See implementation.
- Node.js 23.6 unflags support for running TypeScript files directly, but only permits easily erasable syntax. This flag makes TypeScript error for any syntax that can’t be simply stripped out (e.g., namespaces with runtime code, parameter properties, enums,
--libReplacement
:- TypeScript 4.5 introduced custom library file resolution. The new flag allows you to explicitly disable or enable this behavior to avoid unnecessary lookups and overhead if not needed.
- Discussion on the change.
Declaration File Generation Improvements
- Computed Property Names in Declarations:
- TypeScript 5.8 now more predictably preserves computed property names when emitting declaration files, aligning the declaration output more closely with source code. This also reduces errors in previous versions related to declaration emit.
- However, under the
--isolatedDeclarations
flag, certain computed property naming remains an error. - Implementation details here.
Performance & Behavioral Enhancements
- Program Load and Update Optimizations:
- TypeScript now avoids unnecessary allocations during path normalization and refrains from redundant project option validations, making builds and watch mode more responsive in large projects.
- Pull request for array allocation avoidance.
- Project option check improvements.
- Notable Behavioral Changes:
- DOM type changes in
lib.d.ts
may affect type checking. See related issues. - Import assertions have changed: Node.js 22 and TypeScript 5.8 (with
--module nodenext
) now enforce use ofimport attributes
with thewith
keyword instead ofassert
. -
Example:
// Deprecated import assertion import data from "./data.json" assert { type: "json" }; // Error in TypeScript 5.8 // Preferred import attribute import data from "./data.json" with { type: "json" };
- Change details
- DOM type changes in
Looking Ahead
- Only critical or minor fixes are planned before the final release.
- The TypeScript team will release the stable version and update the iteration plan for scheduling and coordination.
- Users are encouraged to test the RC and submit feedback on GitHub issues.
- For those who want the latest, nightly builds are available via npm; there is also a Visual Studio Code extension for these builds.
Happy Hacking!
— Daniel Rosenwasser and the TypeScript Team
This post appeared first on “Microsoft TypeScript Blog”. Read the entire article here