Repository navigation
perf(bindx-client): declare TEntity invariant on selection builder methods - #150
Merged
Merged
Conversation
…thods ScalarMethod, HasOneMethod and HasManyMethod reference SelectionBuilder again, so TypeScript measures TEntity's variance through a cycle with the `-?` mapped type in SelectionBuilderMethods. Under TypeScript 7.1 that measurement comes out unmeasurable, which forces a structural comparison of every builder: the cost grows with the square of the entity's field count and hits TS2589 on large schemas (microsoft/TypeScript#64950). TypeScript 7.0 was only fast because it measured TEntity as independent through the cycle, which is unsound. `in out` states the variance instead of measuring it. TSelected and THasManyParams need no annotation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ScalarMethod,HasOneMethodandHasManyMethodnow declarein out TEntity.These interfaces return
SelectionBuilderagain, so TypeScript measuresTEntity's variance through a cycle with the-?mapped type inSelectionBuilderMethods. Under TypeScript 7.1 the measurement comes out unmeasurable, so every comparison between two builders falls back to a structural one. That costs instantiations quadratic in the entity's field count, and on large schemas it hitsTS2589: Type instantiation is excessively deep. Reduced repro and bisect: microsoft/TypeScript#64950.TypeScript 7.0 was only fast because it measured
TEntityas independent through the cycle, which is unsound. With the same declarations, 7.0.2 acceptsMethod<{ a: 1 }, 'a', never>asMethod<{ a: 1; b: 1 }, 'a', never>. Declaring the variance avoids measuring it at all.out TEntityis rejected (TS2636):TEntityis not covariant here.in outis not checked by TypeScript and is the conservative choice.TSelectedandTHasManyParamsneed no annotation.Verification
bun run typecheck,bun run test(2198 pass) and eslint on the changed file all pass.On a large downstream app (~2,100 files), checking its main package single-threaded:
No new errors on 7.0.2 or 7.1 anywhere in that app. The Rust and Bun checkers that follow 7.1 also go from failing to 0 errors.
There is no regression test: bindx compiles with TypeScript 5.9, which does not show the slowdown.
🤖 Generated with Claude Code