TypeScript 7.1 Beta
TypeScript 7.1 Beta
These notes cover the 7.1 Beta. Details can change before the stable release. The TypeScript team expects a release candidate about a month after the beta, with the final release shortly after. Source: Announcing TypeScript 7.1 Beta.
TypeScript 7.1 is the second release on the native (Go) compiler. It brings back a programmatic API, which 7.0 shipped without. It also adds a plugin point for Vue, Svelte, Astro and MDX files, plus newer ECMAScript library types. Builds are also faster and use less memory.
npm install -D typescript@betaThe TypeScript API Returns
7.0 had no stable compiler API, so typescript-eslint, framework language tools and anything else that imports typescript stayed on 6.0. 7.1 ships a new API, split into focused entry points:
| Entry point | What it's for |
|---|---|
typescript/sync | Synchronous client: compiler options, programs, the type-checker |
typescript/async | The same client, Promise-based |
typescript/fs | Request-scoped file systems, full or layered |
typescript/ast | AST node types and enums such as SyntaxKind and ScriptTarget |
typescript/ast/is | Node type guards |
typescript/ast/factory | create* and immutable update* node functions |
import { API, formatDiagnosticsWithColorAndContext, type Diagnostic } from "typescript/sync";The new API differs from the 6.0 API in a few ways:
- It doesn't read
tsconfig.jsonfor you. Unliketsc, the API doesn't discover or load a config file and has no--projectequivalent. - Objects are disposable.
APIandProgramobjects implementDisposable. Release them withusing,api.close(),program.dispose(), orapi[Symbol.dispose](). A disposed program can't be reused. - Snapshots handle incremental work.
api.createSnapshot(...)captures state, andsnapshot.update(...)derives a new snapshot from it. PassfileNotificationsto report created, deleted, or changed files. UseensureProgramsto choose which programs get updated. - Emit without touching disk.
program.emitToString()returns emit output as strings.
typescript/fs builds virtual file systems for tests, editors, and build tools:
| Function | Behavior |
|---|---|
createFileSystem(files) | A complete virtual file system. Files you don't list don't exist unless mounted. |
createFileSystemWithLib(files) | The same, with the default lib directory mounted read-only. |
createFileSystemLayer(files) | Overrides only the listed files and falls back to the base layer or the real disk. |
Use removedPaths to block requests from reaching the underlying file system.
Content Mappers: Vue, Svelte, Astro, MDX
A content mapper is a plugin registered for a file extension. It turns a file such as a .vue component into a virtual TypeScript file whose regions map back to the original source. This is the model Volar pioneered, now built into TypeScript. It's aimed at single-file component formats: Vue, Svelte, Astro, MDX, Ember, and CSS Modules.
Content mappers are listed in tsconfig.json, next to compilerOptions:
{
"compilerOptions": {
// ...
},
"contentMappers": [
{
"package": "@vue/content-mapper",
"extensions": [".vue"],
"options": { "target": 3.5 }
}
]
}The announcement notes that this package is an example and isn't published on npm yet.
Content mappers are executables that TypeScript runs, so tsc only loads them when you pass --runExternalCode:
tsc --runExternalCodeIn VS Code, they run once the workspace is trusted and content-mapper permissions are granted. You can turn them off with the js/ts.contentMappers.enabled setting. The TypeScript team has prototype mappers for Astro, CSS Modules, Ember, MDX, Svelte and Vue, plus a sample HTML mapper. The design is written up in microsoft/typescript-go#4712.
Import Attributes in Ambient Modules
Ambient module declarations can now match on import attributes. That means you can type CSS, text, or binary imports by the type you import them with:
declare module "*" with { type: "css" } {
const content: CSSStyleSheet;
export default content;
}
declare module "*" with { type: "text" } {
const content: string;
export default content;
}
declare module "*" with { type: "bytes" } {
const content: Uint8Array;
export default content;
}With these declarations in place:
- Importing a matching file without the attribute is an error.
- The import's type comes from the attribute you use.
- TypeScript doesn't check that the file exists on disk. A missing file fails at runtime or in your bundler.
These declarations don't ship in TypeScript's built-in lib files, and libraries shouldn't publish them. Declare them in your own project.
Source Phase Imports
TypeScript 7.1 supports the proposed source import phase. It gives you a module's compiled source object without linking or running it. That's useful for WebAssembly:
import source SuperFastThing from "./super-fast-thing.wasm";
const instance = await WebAssembly.instantiate(SuperFastThing);There's a dynamic form too:
- Only default imports can use
import source. - The result has the new global type
AbstractModuleSource, declared inlib.esnext.d.ts. - Source imports keep their identity: importing the same
.wasmfile twice gives you the same object. - This requires
--moduleset toesnext,nodenext, orpreserve, andesnextin--libor--target.
Source imports of JavaScript modules aren't useful in practice yet.
--target es2026 and --lib es2026
es2026 is now a stable value for --target and --lib. The target changes little. The library additions matter more:
Math.sumPrecise()Iterator.concat()JSON.rawJSON(), and aJSON.parse()overload that gives revivers the original source textError.isError()Map.prototype.getOrInsert()andgetOrInsertComputed(), plus the same onWeakMapUint8Array.prototype.toBase64(),setFromBase64(),toHex(),setFromHex()Uint8Array.fromBase64()andUint8Array.fromHex()Array.fromAsync()
const counts = new Map<string, number>();
counts.set("a", counts.getOrInsert("a", 0) + 1);
Math.sumPrecise([0.1, 0.2, 0.3]); // 0.6, not 0.6000000000000001New esnext Library Methods
--lib esnext adds types for these proposals:
Promise.allKeyedandPromise.allSettledKeyedIterator.zipandIterator.zipKeyedIterator.prototype.chunksIterator.prototype.joinIterator.prototype.includes
Faster Builds, Less Memory
7.0 was already about 10x faster than 6.0. 7.1 adds dozens of optimizations across the checker and tsc -b scheduling. Projects now build in dependency-depth order. These are the beta's numbers against 7.0, with the default 4 checker threads and --noEmit:
| Codebase | 7.0 | 7.1 Beta | Speedup | Memory 7.0 → 7.1 Beta |
|---|---|---|---|---|
| bluesky | 4.48s | 3.89s | 1.2x | 2,017 → 1,698 MiB (−15.8%) |
| mui-docs | 22.2s | 11.9s | 1.9x | 8,093 → 4,079 MiB (−49.6%) |
| sentry | 19.6s | 15.1s | 1.3x | 6,587 → 5,216 MiB (−20.8%) |
| t3code-server | 26.5s | 19.1s | 1.4x | 6,894 → 5,197 MiB (−24.6%) |
| vscode | 21.6s | 17.7s | 1.2x | 8,387 → 6,943 MiB (−17.2%) |
Notable Behavioral Changes
lib.d.ts updates
The DOM types are regenerated from current web specs. As in most releases, this can surface new errors where browser APIs changed shape. See microsoft/TypeScript#63702.
Stricter checks on distributive type parameters
Inside each branch of a distributive conditional type, TypeScript 7.1 treats the checked type parameter as a new, narrower type parameter. It's still a subtype of the original, but it no longer carries the original's relationships to other type parameters. Code that relied on those relationships can now report errors such as "Type B does not satisfy the constraint A".
To fix it, remove constraints the branch doesn't need, or add a conditional check that re-establishes the constraint. Tracked in microsoft/TypeScript#63708.