TypeScript 4.7 Beta Feature Overview
TypeScript 4.7 beta, released April 8 2022, introduces Node.js ES‑module support, module‑detection control, computed‑property flow analysis, generic instantiation expressions, infer‑extends constraints, variance annotations, private‑field typeof, custom module suffixes, import‑assertion resolution modes, and assorted editor and type‑system enhancements while tightening some compatibility checks.
TypeScript 4.7 beta was released on 2022‑04‑08. You can try the new features early by running $ npm install typescript@beta or by installing the JavaScript and TypeScript Nightly extension in VS Code.
The previous 4.6 release introduced heuristic recursive type checking, index‑access type inference and improved control‑flow analysis for function parameters. For example, deep nested utility types are now considered compatible after a limited recursive check:
interface Foo
{ prop: T; }
declare let x: Foo
>>>>>>;
declare let y: Foo
>>>>>>;
x = y; // allowed by heuristic checkVersion 4.7 adds many new capabilities:
Node.js ES‑Module support : the node12 and nodenext module options now respect the type field in package.json and require explicit file extensions for relative imports.
Module detection control via the moduleDetection compiler option (values: "auto" , "force" , "legacy" ).
Computed‑property control‑flow analysis – the type of obj[key] is narrowed correctly inside if (typeof obj[key] === "string") blocks.
Generic instantiation expressions allow pre‑filling type arguments without a call, e.g.:
// Not a type alias, but an instantiated generic
const ErrorMap = Map
;
const errorMap = new ErrorMap();These expressions can also be used with typeof :
type ErrorMapConstructor = typeof Map
;Infer extends constraints simplify conditional types, e.g.:
type FirstString
= T extends [infer S extends string, ...unknown[]] ? S : never;Variance annotations ( in and out ) let you declare covariant or contravariant generic parameters:
type Getter
= () => T;
type Setter
= (value: T) => void;Private fields declared with # now support typeof :
class Example { #esPrivateProp = "hello"; }
const p: typeof Example.prototype.#esPrivateProp = "world";A new moduleSuffixes compiler option enables custom module‑resolution order (e.g. ['.ios', '.native', ''] ), useful for React Native or platform‑specific code.
Import assertions can specify the resolution mode, allowing separate type imports from CommonJS and ES modules:
import type { TypeFromRequire } from "pkg" assert { "resolution-mode": "require" };
import type { TypeFromImport } from "pkg" assert { "resolution-mode": "import" };Import statements are now grouped according to preceding comments, preserving logical sections in the emitted JavaScript.
Object literal methods receive full snippet completions, and readonly tuples now have a readonly length property.
These changes, along with several breaking updates (e.g., stricter generic‑parameter compatibility under strictNullChecks ), are summarized in the TypeScript 4.7 beta dev‑blog.
DaTaobao Tech
Official account of DaTaobao Technology
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.