0Pricing
TypeScript Academy · Lesson

TypeScript 5.3-5.4: New Narrowing and NoInfer

Apply the NoInfer utility and new switch/case narrowing.

TypeScript 5.3-5.4: New Narrowing and NoInfer is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.3

Switch/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;
  }
}

JSDoc import type

TS 5.3 added support for @import JSDoc tag for type-only imports in JavaScript files.

/** @import { User } from "./types.js" */

/** @param {User} user */
function greet(user) {
  console.log(user.name);
}

resolution-mode in import type

TS 5.3 allows specifying resolution-mode in import type to control whether the import resolves as CommonJS or ESM.

import type { User } from "some-library" with { "resolution-mode": "require" };

TypeScript 5.4 Overview

TypeScript 5.4 (March 2024) introduced the NoInfer utility type, preserved narrowing in closures, and Object.groupBy and Map.groupBy types.

// npm install typescript@5.4

NoInfer<T> Utility Type

NoInfer prevents TypeScript from using a generic argument as an inference site, forcing the type to be inferred from other arguments only.

function createStore<T>(initial: T, onChange: (val: NoInfer<T>) => void) {
  // T inferred from initial, not from onChange callback
}

createStore({ count: 0 }, (val) => {
  // val: { count: number } — inferred from initial only
});

Why NoInfer Matters

Without NoInfer, TypeScript would try to infer T from both arguments and might widen the type unexpectedly.

// Before NoInfer: T may be widened by the callback
function createStore<T>(initial: T, onChange: (val: T) => void) {}
createStore({ count: 0 }, (val: { count: number; extra: string }) => {});
// T widened to include extra — not what we want

Preserved Narrowing After Closures

TS 5.4 preserves type narrowing in closures when the narrowed variable is not assigned to after the closure.

function process(val: string | null) {
  if (!val) return;
  // val: string
  setTimeout(() => {
    console.log(val.toUpperCase()); // TS 5.4: still string — not widened
  }, 100);
}

Object.groupBy Typing

TS 5.4 added types for the ES2024 Object.groupBy and Map.groupBy static methods.

const users = [
  { name: "Alice", role: "admin" },
  { name: "Bob", role: "user" },
];
const grouped = Object.groupBy(users, (u) => u.role);
// grouped: { [role: string]: { name: string; role: string }[] }

Improved Declaration Emit

TS 5.4 improved declaration file emit for complex cases involving mapped types and conditional types, reducing false errors in generated .d.ts files.

// Fewer "Type ... is not assignable to type ..." errors
// in generated declaration files from TS 5.4 onward

Recap: TS 5.3-5.4

TS 5.3: improved switch/case narrowing, JSDoc @import support. TS 5.4: NoInfer to block unwanted type inference sites, preserved closure narrowing, and Object.groupBy types.

Quick Check

What does NoInfer prevent?

What You Learned

TS 5.3 improved switch/case narrowing and added JSDoc import types. TS 5.4 introduced NoInfer to control inference sites, preserved narrowing across closures, and added Object.groupBy types for ES2024.

Frequently asked questions

Is the “TypeScript 5.3-5.4: New Narrowing and NoInfer” lesson free?

Yes — the full text of “TypeScript 5.3-5.4: New Narrowing and NoInfer” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “TypeScript 5.3-5.4: New Narrowing and NoInfer”?

Apply the NoInfer utility and new switch/case narrowing. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “TypeScript 5.3-5.4: New Narrowing and NoInfer” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. TypeScript 5.0: Decorators Standard and const Type Params
  2. TypeScript 5.1-5.2: Improved Inference
  3. TypeScript 5.3-5.4: New Narrowing and NoInfer
  4. TypeScript 5.5+: Isolated Declarations and Beyond
← Back to TypeScript Academy