Entry: Export ObjectEntry and ObjectEntries types#1361
Open
derodero24 wants to merge 2 commits intosindresorhus:mainfrom
Open
Entry: Export ObjectEntry and ObjectEntries types#1361derodero24 wants to merge 2 commits intosindresorhus:mainfrom
Entry: Export ObjectEntry and ObjectEntries types#1361derodero24 wants to merge 2 commits intosindresorhus:mainfrom
Conversation
| expectType<Array<['a' | 'b', string | number]>>(genericEntries); | ||
|
|
||
| declare const genericEntry: ReturnType<typeof getEntry<Example>>; | ||
| expectType<['a' | 'b', string | number]>(genericEntry); |
Owner
There was a problem hiding this comment.
Suggested change
| expectType<['a' | 'b', string | number]>(genericEntry); | |
| expectType<['a' | 'b', string | number]>(genericEntry); | |
| // Entry and Entries can widen in generic object contexts because they are conditional over collection kinds. | |
| type EntryAssignability<BaseType extends Record<string, unknown>, EntryType extends [keyof BaseType, BaseType[keyof BaseType]]> = EntryType; | |
| // @ts-expect-error -- Entry<BaseType> can widen to the generic constraint when BaseType is generic. | |
| type EntryAssignabilityTest<BaseType extends Record<string, unknown>> = EntryAssignability<BaseType, Entry<BaseType>>; | |
| type EntriesAssignability<BaseType extends Record<string, unknown>, EntriesType extends Array<[keyof BaseType, BaseType[keyof BaseType]]>> = EntriesType; | |
| // @ts-expect-error -- Entries<BaseType> can widen to the generic constraint when BaseType is generic. | |
| type EntriesAssignabilityTest<BaseType extends Record<string, unknown>> = EntriesAssignability<BaseType, Entries<BaseType>>; |
Contributor
Author
There was a problem hiding this comment.
Applied the suggestion — thanks for pointing out this case. These tests clearly document why ObjectEntry/ObjectEntries are needed as direct alternatives.
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 #649
Summary
ObjectEntryandObjectEntriesas public types that can be used directly with object typesEntry/Entriestypes with generic type parameters@exampleand@seecross-referencesContext
The
Entries<T>type uses conditional type dispatch (T extends Map ? ... : T extends Set ? ... : ...), which causes TypeScript to lose type information whenTis a generic type parameter. The internalObjectEntry/ObjectEntriestypes don't have this limitation since they work directly with object types without conditional dispatch. Exporting them gives users a way to get properly typedObject.entries()results in generic contexts.