fix(pick): return Partial<Pick<T, K>> for non-tuple key arrays#1634
Open
Seojooyeon-creat wants to merge 4 commits intotoss:mainfrom
Open
fix(pick): return Partial<Pick<T, K>> for non-tuple key arrays#1634Seojooyeon-creat wants to merge 4 commits intotoss:mainfrom
Seojooyeon-creat wants to merge 4 commits intotoss:mainfrom
Conversation
…tion Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Closes #1623
Summary
pickcurrently returnsPick<T, K>regardless of whetherkeysis a tuple or a variable-length array. This is incorrect whenKis a union type:Pick<T, 'a' | 'b'>implies bothaandbare always present, but the runtime value may only contain one of them.Changes
src/object/pick.tsChanged the signature from:
to:
readonly [...Keys]forces TypeScript to infer tuple types for array literals like['a', 'b']number extends Keys['length']distinguishes tuple (length is a specific literal) from variable-length array (length isnumber)Pick<T, K>, variable-length array →Partial<Pick<T, K>>src/object/pick.spec.tsAdded two type-level tests using
expectTypeOf:['a', 'b']→Pick<T, 'a' | 'b'>(all keys guaranteed)('a' | 'b')[]→Partial<Pick<T, 'a' | 'b'>>(only present keys)docs/(en, ko, ja, zh_hans)Updated parameter and return type descriptions to reflect the new behavior.
Context
A similar fix was previously applied to
omit(#1498) using overloads and then reverted (#1595). This PR takes a different approach — a single signature with a conditional return type — which avoids the added complexity of overloads while correctly narrowing the type based on tuple vs. array input.Benchmark
Runtime logic is unchanged. Performance is maintained: