Skip to content

perf(hotkeys): Keep defineAppHotkeys fast to edit in large declarations - #327

Merged
KevinVandy merged 2 commits into
mainfrom
perf/define-app-hotkeys-editor
Nov 29, 2026
Merged

KevinVandy merged 2 commits into
mainfrom
perf/define-app-hotkeys-editor

Conversation

@KevinVandy

@KevinVandy KevinVandy commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

🎯 Changes

Typing a hotkey string inside a large defineAppHotkeys call made editors lag: up to 0.6 seconds per keystroke in Supabase Studio's appHotkeys.ts, and up to 2 seconds in a 300-entry call. tsc barely shows it, because the cost is in work the language service repeats on every edit.

While TypeScript infers the declarations, it checks every literal in the call against contextual types built from the generic's constraint, and each check walks any union it finds (isConstTypeVariable, mapType). The constraint was Record<string, AppHotkeyInput>, whose hotkey holds the 6,720-member chord union, so every chord string and sequence step walked it on every keystroke.

  • defineAppHotkeys infers against AppHotkeyDeclaration, a new exported shape whose hotkey names no hotkey union. It includes a private UncheckedHotkey marker, so the unresolved constraint can be told apart from any value someone writes.
  • ValidatedAppHotkeyInputs checks each hotkey after inference. A string is checked against the chord union and a sequence step by step. While inference runs, the unresolved constraint counts as valid, so the error branch and its Hotkey | null are never built for a contextual type. Invalid entries are still reported against Hotkey | null with TypeScript's "Did you mean" suggestion, and options are checked against the kind on both branches.
  • Hover docs stay: checked entries are intersected with a docs-only interface, so hovering hotkey, meta, scope, or options still shows their documentation.
  • Fixes TS2590 from feat(hotkeys): Keep default kinds of app hotkeys spread into defineAppHotkeys #325. With spread-in entries, a generic lookup such as UseAppHotkeyOptions<RegisteredAppHotkeys[TId]> failed with "Expression produces a union type that is too complex to represent", which breaks Supabase Studio's <Shortcut> component on 0.40.1. The spread-in kind check read TInput['hotkey'] inside the AppHotkey branch, where TypeScript intersects it with Hotkey | null; it now reads the hotkey with infer.
  • Tests: app-hotkeys-editor-latency.test.ts types a hotkey through the language service and fails if defineAppHotkeys is more than 4× slower than the same 300 declarations checked with as const satisfies (main: 8.4×, this branch: 0.7–0.8×). It takes about 6 seconds locally and allows 3 minutes for slow CI runners. It also asserts the call was checked. New type tests cover the generic lookup, conditional and ?? hotkeys, nullable replacements of spread-in hotkeys, and values that are not hotkeys.

Worst keystroke while typing 'Mod+Shift+K' into one entry (language service, TypeScript 6.0.3):

Declarations main This PR
30 238 ms 131 ms
100 750 ms 171 ms
300 2,028 ms 258 ms
1,000 6,735 ms 527 ms
Supabase Studio appHotkeys.ts (~300, 36 files spread in) 617 ms 280 ms

The same 300 declarations checked with as const satisfies take 251 ms. With this build, Supabase Studio type-checks (it fails on 0.40.1) and its 1,196 shortcut, UI, grid, and preferences tests pass.

Types only: the emitted JavaScript of app-hotkeys.ts is identical to main, so there is no bundle size change.

Notes

These type behaviors change, and they are worth a look before this merges:

  • A union of declaration objects, such as defineAppHotkeys(flag ? a : {}), no longer type-checks; call defineAppHotkeys in each branch. A generic function that passes its argument to defineAppHotkeys now type-checks, where main rejected it.
  • Completions list every chord while the text is empty or doesn't yet form a chord. Once it does, such as 'M' or 'Mod+S', completions at that position list only the current text; main listed every chord.
  • A hotkey that is not a string, array, or null, such as 5, is reported against string | readonly string[] | UncheckedHotkey | null instead of Hotkey | null. DefinedAppHotkeys<T> used on its own no longer rejects invalid hotkeys; defineAppHotkeys does.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

defineAppHotkeys inferred declarations against a constraint holding the
chord union, and the language service walked that union for every literal
in the call on every keystroke. It now infers against AppHotkeyDeclaration,
which names no hotkey union, and checks each hotkey after inference.

Also fixes TS2590 for generic lookups over spread-in app hotkeys.
@changeset-bot

changeset-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8d233d4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@tanstack/hotkeys Patch
@tanstack/alpine-hotkeys Patch
@tanstack/angular-hotkeys Patch
@tanstack/ember-hotkeys Patch
@tanstack/hotkeys-devtools Patch
@tanstack/lit-hotkeys Patch
@tanstack/octane-hotkeys Patch
@tanstack/preact-hotkeys Patch
@tanstack/react-hotkeys Patch
@tanstack/solid-hotkeys Patch
@tanstack/svelte-hotkeys Patch
@tanstack/vue-hotkeys Patch
@tanstack/angular-hotkeys-devtools Patch
@tanstack/preact-hotkeys-devtools Patch
@tanstack/react-hotkeys-devtools Patch
@tanstack/solid-hotkeys-devtools Patch
@tanstack/svelte-hotkeys-devtools Patch
@tanstack/vue-hotkeys-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

1 package(s) bumped directly, 17 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/hotkeys 0.40.1 → 0.40.2 Changeset
@tanstack/alpine-hotkeys 0.17.4 → 0.17.5 Dependent
@tanstack/angular-hotkeys 0.27.4 → 0.27.5 Dependent
@tanstack/angular-hotkeys-devtools 0.5.24 → 0.5.25 Dependent
@tanstack/ember-hotkeys 0.16.4 → 0.16.5 Dependent
@tanstack/hotkeys-devtools 1.13.4 → 1.13.5 Dependent
@tanstack/lit-hotkeys 0.29.4 → 0.29.5 Dependent
@tanstack/octane-hotkeys 0.17.4 → 0.17.5 Dependent
@tanstack/preact-hotkeys 0.26.4 → 0.26.5 Dependent
@tanstack/preact-hotkeys-devtools 0.13.24 → 0.13.25 Dependent
@tanstack/react-hotkeys 0.29.4 → 0.29.5 Dependent
@tanstack/react-hotkeys-devtools 0.14.24 → 0.14.25 Dependent
@tanstack/solid-hotkeys 0.28.4 → 0.28.5 Dependent
@tanstack/solid-hotkeys-devtools 0.14.9 → 0.14.10 Dependent
@tanstack/svelte-hotkeys 0.27.4 → 0.27.5 Dependent
@tanstack/svelte-hotkeys-devtools 0.5.24 → 0.5.25 Dependent
@tanstack/vue-hotkeys 0.27.4 → 0.27.5 Dependent
@tanstack/vue-hotkeys-devtools 0.13.24 → 0.13.25 Dependent

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/alpine-hotkeys

npm i https://pkg.pr.new/@tanstack/alpine-hotkeys@327

@tanstack/angular-hotkeys

npm i https://pkg.pr.new/@tanstack/angular-hotkeys@327

@tanstack/angular-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/angular-hotkeys-devtools@327

@tanstack/ember-hotkeys

npm i https://pkg.pr.new/@tanstack/ember-hotkeys@327

@tanstack/hotkeys

npm i https://pkg.pr.new/@tanstack/hotkeys@327

@tanstack/hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/hotkeys-devtools@327

@tanstack/lit-hotkeys

npm i https://pkg.pr.new/@tanstack/lit-hotkeys@327

@tanstack/octane-hotkeys

npm i https://pkg.pr.new/@tanstack/octane-hotkeys@327

@tanstack/preact-hotkeys

npm i https://pkg.pr.new/@tanstack/preact-hotkeys@327

@tanstack/preact-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/preact-hotkeys-devtools@327

@tanstack/react-hotkeys

npm i https://pkg.pr.new/@tanstack/react-hotkeys@327

@tanstack/react-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/react-hotkeys-devtools@327

@tanstack/solid-hotkeys

npm i https://pkg.pr.new/@tanstack/solid-hotkeys@327

@tanstack/solid-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/solid-hotkeys-devtools@327

@tanstack/svelte-hotkeys

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys@327

@tanstack/svelte-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys-devtools@327

@tanstack/vue-hotkeys

npm i https://pkg.pr.new/@tanstack/vue-hotkeys@327

@tanstack/vue-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/vue-hotkeys-devtools@327

commit: 8d233d4

It timed out at 60 seconds on CI, about ten times slower than locally.
Take the best of two interleaved runs, allow 3 minutes, and fail above 4x
the satisfies baseline (main measures 8.4x).
@KevinVandy
KevinVandy marked this pull request as ready for review November 29, 2026 23:15
@KevinVandy
KevinVandy merged commit 0b0e4fa into main Nov 29, 2026
17 of 18 checks passed
@KevinVandy
KevinVandy deleted the perf/define-app-hotkeys-editor branch November 29, 2026 23:18
@github-actions github-actions Bot mentioned this pull request Nov 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR has been released!

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant