Skip to content

Comments

Entry: Export ObjectEntry and ObjectEntries types#1361

Open
derodero24 wants to merge 2 commits intosindresorhus:mainfrom
derodero24:fix/export-object-entries
Open

Entry: Export ObjectEntry and ObjectEntries types#1361
derodero24 wants to merge 2 commits intosindresorhus:mainfrom
derodero24:fix/export-object-entries

Conversation

@derodero24
Copy link
Contributor

Closes #649

Summary

  • Export ObjectEntry and ObjectEntries as public types that can be used directly with object types
  • These types preserve generic type information that would otherwise be lost when using the conditional Entry/Entries types with generic type parameters
  • Add JSDoc documentation with @example and @see cross-references
  • Add tests verifying both direct usage and generic type parameter preservation

Context

The Entries<T> type uses conditional type dispatch (T extends Map ? ... : T extends Set ? ... : ...), which causes TypeScript to lose type information when T is a generic type parameter. The internal ObjectEntry/ObjectEntries types don't have this limitation since they work directly with object types without conditional dispatch. Exporting them gives users a way to get properly typed Object.entries() results in generic contexts.

expectType<Array<['a' | 'b', string | number]>>(genericEntries);

declare const genericEntry: ReturnType<typeof getEntry<Example>>;
expectType<['a' | 'b', string | number]>(genericEntry);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>>;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggestion — thanks for pointing out this case. These tests clearly document why ObjectEntry/ObjectEntries are needed as direct alternatives.

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.

Entries don't works like ObjectEntries

2 participants