Making my TypeScript types 15.7x faster
Описание и применение
Автор оптимизировал библиотеку схем Sury, уменьшив количество инстанцирований типов в 11.2–15.7 раз. Решение заключалось в изменении типа S.Output, чтобы он читал тип напрямую из поля ~standard, вместо того чтобы расширять весь сложный тип Schema<…>.
Задача
Large TypeScript types cause type inference slowdowns, making compilation painfully slow and increasing the cost of type-checking operations like extraction.
Результат
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.
Рабочий процесс
- 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/attestto 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.
Доказательства
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:
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:
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.
- Дата записи: 2026-09-08T05:14:59.988Z