0Pricing
TypeScript Academy · Lesson

TypeScript 5.1-5.2: Improved Inference

Explore setter/getter type independence and using declarations.

TypeScript 5.1 Overview

TypeScript 5.1 (released June 2023) improved setter/getter type independence, JSX element type flexibility, linked cursors in editors, and performance optimizations.

// npm install typescript@5.1

Setter/Getter Type Independence

Before 5.1, setter and getter had to use the same type. Now, a getter can return a wider type than the setter accepts — useful for coercion patterns.

class Temperature {
  get celsius(): string { return this._c + "°C"; } // string
  set celsius(value: string | number) {             // string | number
    this._c = Number(value);
  }
  private _c = 0;
}

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