TypeScript 5.3-5.4: New Narrowing and NoInfer
Apply the NoInfer utility and new switch/case narrowing.
TypeScript 5.3 Overview
TypeScript 5.3 (November 2023) brought improved narrowing for switch/case statements, JSDoc import type support, and resolution mode in import type.
// npm install typescript@5.3Switch/Case Narrowing Improvement
TS 5.3 improved type narrowing within switch statements, especially for discriminated unions with multiple cases.
type Result = { status: "ok"; data: string } | { status: "err"; code: number };
function handle(r: Result) {
switch (r.status) {
case "ok":
console.log(r.data); // r: { status: "ok"; data: string } — narrowed
break;
case "err":
console.log(r.code); // r: { status: "err"; code: number } — narrowed
break;
}
}All lessons in this course
- TypeScript 5.0: Decorators Standard and const Type Params
- TypeScript 5.1-5.2: Improved Inference
- TypeScript 5.3-5.4: New Narrowing and NoInfer
- TypeScript 5.5+: Isolated Declarations and Beyond