Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Making my TypeScript types 15.7x faster

Description and use

The author optimized the Sury schema library, reducing type instantiations by 11.2x to 15.7x. The solution involved changing the S.Output type to read the type directly from the ~standard field instead of expanding the entire complex Schema<…> type.

Problem

Large TypeScript types cause type inference slowdowns, making compilation painfully slow and increasing the cost of type-checking operations like extraction.

Outcome

By changing the S.Output / S.Input type definition to match only the ~standard field instead of the entire Schema<…> shape, the author achieved an 11.2x to 15.7x reduction in type instantiations for schema extraction operations.

Workflow

  • Pin down the inferred type using type-testing utilities (e.g., Vitest's expectTypeOf) to ensure type optimizations do not silently break the schema's contract.
  • Measure the type instantiation cost of the schema using @ark/attest to establish a baseline and identify performance bottlenecks.
  • Ask an AI (Claude) to find the most impactful type-instantiation optimizations, which led to a targeted fix.

Evidence

The fix turned out to be small. To read a schema's type back out, S.Output / S.Input matched against the *entire* Schema<…> type. That type is huge: a big object with several with overloads, intersected with a union of every schema variant. So TypeScript had to expand all of it on every extraction, and the cost grew with the schema:

Source

But every Sury schema already carries a ~standard field (the Standard Schema marker) that holds the resolved types. So the fix is to read that one slot directly, instead of re-deriving it from the whole shape:

Source

The title comes from that **15.7×** on S.Output. Extraction is the operation you hit most often: every type X = S.Output<typeof schema> in your code pays for it.

Source

  • Record date: 2026-09-08T05:14:59.988Z