Skip to content

stable-shape ​

Objects that get properties after they were created, so objects of the same kind end up with different sets of keys.

Why: V8 gives each set of keys (in order) a hidden class. Code that sees one hidden class is fast; code that sees several has to check, and past four gives up on the fast path (v8.dev, v8.dev).

❌ Rejected: four combinations of badge and hint, four shapes

✕ Rejectedstable-shape
ts
export function cell(value: number, limit: number) {
  return {
    value,
    ...(value < 0 ? { badge: "negative" } : {}),
    ...(value > limit ? { hint: `over ${limit}` } : {}),
  };
}

✅ Accepted: every result has the same keys

✓ Accepted0 diagnostics
ts
export function cell(value: number, limit: number) {
  return {
    value,
    badge: value < 0 ? "negative" : undefined,
    hint: value > limit ? `over ${limit}` : undefined,
  };
}

It also reports o.x = ... on an object literal that didn't have x, also when the literal has conditional spreads (their keys count as its keys), and, in JavaScript files, this.x = ... set on only some paths of a constructor, or only in a method. (TypeScript already rejects most of these: the property isn't declared.)

Leaving out keys ​

With exactOptionalPropertyTypes, an optional property (badge?: string) can't be set to undefined: the key must be missing. So one conditional spread of a one-key literal is allowed, ...(c ? { key: v } : {}) (or ...(c && { key: v })). It makes exactly two shapes, the same as a ternary between two whole literals, and is easier to read:

✓ Accepted0 diagnostics
ts
export type Cell = { readonly value: number; readonly badge?: string };

export function cell(value: number): Cell {
  return { value, ...(value < 0 ? { badge: "negative" } : {}) };
}

Two keys left out by conditional spreads are accepted where the object's type declares both optional (key?: T): the type already says these objects come in several shapes, and two keys make at most four. More are reported whatever the type says: each key doubles the shapes, and three make eight, past what V8 keeps fast for reads of these objects (Shapes). The shapes come from the literal's spreads together, so a literal gets one finding, at its first conditional spread, however many it has (and one safe-ts-ignore covers it). The type is the one the literal is written for: a variable's annotation, the function's return type, satisfies T, the parameter of the function it is passed to (here or imported), the element type of the array it is pushed to, or, for a literal written as a field's value (open({ callbacks: { ...(a ? { any: a } : {}) } })), that field's type in the outer one. It is followed through aliases, interfaces, Partial, Omit, Pick, intersections and unions (the member the literal's type: "..." picks):

✓ Accepted0 diagnostics
ts
export type Receipt = { readonly id: string; readonly note?: string; readonly coupon?: string };

export function receipt(id: string, note: string, coupon: string): Receipt {
  return { id, ...(note !== "" && { note }), ...(coupon !== "" ? { coupon } : {}) };
}

❌ Rejected: three optional keys, eight shapes, even though the type declares them

✕ Rejectedstable-shape
ts
export type Receipt = { readonly id: string; readonly note?: string; readonly coupon?: string; readonly gift?: boolean };

export function receipt(id: string, note: string, coupon: string, gift: boolean): Receipt {
  return { id, ...(note !== "" && { note }), ...(coupon !== "" ? { coupon } : {}), ...(gift && { gift }) };
}

The message says how many keys the literal leaves out and how many shapes that makes. For three keys or more, or where the objects are read in a hot loop, make the keys always present, note: Option<string> set to undefined when there is none: every object then has one shape, at the cost of writing note: undefined where one is made. Adding the keys after the literal (receipt.note = note) isn't a way out: it makes as many shapes, and is reported too.

✅ Accepted: three keys, always written, one shape

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

export type Receipt = {
  readonly id: string;
  readonly note: Option<string>;
  readonly coupon: Option<string>;
  readonly gift: boolean;
};

export function receipt(id: string, note: string, coupon: string, gift: boolean): Receipt {
  return {
    id,
    note: note !== "" ? note : undefined,
    coupon: coupon !== "" ? coupon : undefined,
    gift,
  };
}

Spreading a value with different keys does the same, and is reported the same way: a const holding a conditional literal (const extra = gift ? { gift, wrap: true } : {}), or the result of a function (here or imported) whose returns give literals with different keys:

✕ Rejectedstable-shape
ts
function noteOf(note: string) {
  return note !== "" ? { note } : {};
}

export function receipt(id: string, note: string, gift: boolean) {
  const extra = gift ? { gift, wrap: true } : {};
  return { id, ...noteOf(note), ...extra };
}

One function, more than four shapes ​

The rule also reports a parameter that gets object literals with more than four different lists of keys (a different set, or the same keys in another order) from the calls in its file, when the function reads the parameter's properties in a loop or an array callback.

❌ Rejected: five calls, five shapes, one loop that reads them

✕ Rejectedstable-shape
ts
export type Batch = { readonly items: readonly number[]; readonly scale: number; readonly name?: string };

function weigh(batch: Batch): number {
  let sum = 0;
  for (const item of batch.items) sum += item * batch.scale;
  return sum;
}

export function total(items: readonly number[], n: number): number {
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += weigh({ items, scale: i });
    sum += weigh({ scale: i, items });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ name: "a", items, scale: i });
    sum += weigh({ items, name: "a", scale: i });
  }
  return sum;
}

✅ Accepted: the same keys, in the same order, everywhere

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

export type Batch = { readonly items: readonly number[]; readonly scale: number; readonly name: Option<string> };

function weigh(batch: Batch): number {
  let sum = 0;
  for (const item of batch.items) sum += item * batch.scale;
  return sum;
}

export function total(items: readonly number[], n: number): number {
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += weigh({ items, scale: i, name: undefined });
    sum += weigh({ items, scale: i, name: undefined });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ items, scale: i, name: "a" });
  }
  return sum;
}
Benchmark total(items, 200)
Code that runs

Each version runs on its own, in a fresh copy of this code (TypeScript types are removed first). The setup runs once per version; the last line is what is timed.

Five shapes
export type Batch = { readonly items: readonly number[]; readonly scale: number; readonly name?: string };

function weigh(batch: Batch): number {
  let sum = 0;
  for (const item of batch.items) sum += item * batch.scale;
  return sum;
}

export function total(items: readonly number[], n: number): number {
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += weigh({ items, scale: i });
    sum += weigh({ scale: i, items });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ name: "a", items, scale: i });
    sum += weigh({ items, name: "a", scale: i });
  }
  return sum;
}
One shape
import type { Option } from "@evobug_com/safe-ts-std/result";

export type Batch = { readonly items: readonly number[]; readonly scale: number; readonly name: Option<string> };

function weigh(batch: Batch): number {
  let sum = 0;
  for (const item of batch.items) sum += item * batch.scale;
  return sum;
}

export function total(items: readonly number[], n: number): number {
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += weigh({ items, scale: i, name: undefined });
    sum += weigh({ items, scale: i, name: undefined });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ items, scale: i, name: "a" });
    sum += weigh({ items, scale: i, name: "a" });
  }
  return sum;
}
Setup and timed expression
const items = Array.from({ length: 20 }, (_, i) => i);

// timed:
total(items, 200);

Warm: median time per run once V8 has optimized the code (p10–p90: the spread of the samples). First run: the very first call, before any optimization, which is closer to code that runs rarely. Measured in a Web Worker in your browser; results depend on the engine and the machine, which is the point of running it here.
Size: Five shapes 604 B (206 B gzip) · One shape 708 B (199 B gzip).

Measured on Node 26: 65–113 µs for the 1,000 calls with five shapes against 11–19 µs with one, 4–7x (eight runs).

The loop matters. A small function called with five shapes and reading each property once is inlined into its caller, where V8 knows each literal's shape, and runs as fast as with one (measured: no difference), so that case isn't reported.

Can't see: calls from other files, objects passed in variables (const b = {...}; weigh(b)), literals with spreads or computed keys, methods (obj.weigh(...)), and objects that are stored and read later by other code (about 1.6x in a similar measurement). Seeing any of them could only add shapes, so what is reported is a lower bound.

Not reported on purpose: a parameter whose type is a union of more than four object types, such as an Enum with five variants read with match or a switch on .tag. Those reads do go megamorphic, but the variants are different on purpose, and giving them all one shape would give up the types that make match exhaustive. If such a read sits in your hottest loop, measure it.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra