fix(type-plus): keep ? and readonly of A in LeftJoin - #710
Merged
Merged
Conversation
The general branch mapped A's non-colliding keys over `Exclude<keyof A, keyof B>`, which is not homomorphic, so optional and readonly modifiers were dropped. Filter with a key-remapping `as` clause over `keyof A` instead, which keeps them. On a collision B still wins, modifiers included. Fixes #205 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 96934ef The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #710 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 43 43
Lines 231 231
Branches 51 51
=========================================
Hits 231 231 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #205.
Problem
LeftJoin<{ a?: number; b?: string }, { b: boolean }>returned{ a: number; b: boolean }. The general branch mappedA's non-colliding keys with{ [k in Exclude<keyof A, keyof B>]: A[k] }. That key set is computed, so the mapped type is not homomorphic and drops?andreadonly.Fix
{ [k in keyof A as k extends keyof B ? never : k]: A[k] }. A key-remappingasclause overkeyof Akeeps the mapped type homomorphic, soA's modifiers survive.Collision rule: when both sides declare a key,
Breplaces the whole property, including its?andreadonly. This matches the existing rule thatBwins on the type, and it is what theBhalf of the join already did. The TSDoc now states the rule.The two short-circuits (
IsEqual→A,IsDisjoint→A & B) already kept modifiers. They are now covered by spec tests, so all three branches are pinned to the same behavior.Tests
left_join.spec.ts:A's modifiers on keysBdoes not redeclare,B's modifiers, the collision case, and both short-circuits. The two new TSDoc@examples are pinned there.A-modifier cases fail against the old implementation and pass with the fix.test:typepasses on TypeScript 5.4, 5.5, 5.6, 6.0 and 7.verify:pkg,biome checkanddocs:llms:checkpass. Error snapshots are unchanged.Changeset: patch
I chose
patch. This is a defect fix: the output now matches what a left join is expected to produce, and other type-output fixes in this release line are also patches. The package is in the8.0.0-betapre-release, so a larger bump would not change the version line anyway. It can still break a consumer that relied on the wrong output, for example code that read a formerly optional property as required. If the Council wants that treated as breaking, change the bump tomajor.🤖 Generated with Claude Code