Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Strongly-typed version of Object.values(). And synchronize objectKeys and objectEntries #65

Open
aandrejevas opened this issue Aug 22, 2024 · 0 comments

Comments

@aandrejevas
Copy link

aandrejevas commented Aug 22, 2024

It would be useful to add a strongly-typed version of Object.values().

How Object.values is defined:

values<T>(o: { [s: string]: T; } | ArrayLike<T>): T[];

How objectValues could be defined:

export const objectValues = Object.values as <Type extends object>(value: Type) => Array<Required<Type>[ObjectKeys<Type>]>;

Also objectKeys and objectEntries definitions should be synchronized:

// Notice "Type extends Record<PropertyKey, unknown>". This definition rejects interfaces.
export const objectEntries = Object.entries as <Type extends Record<PropertyKey, unknown>>(value: Type) => Array<[ObjectKeys<Type>, Required<Type>[ObjectKeys<Type>]]>;

// Notice "Type extends object". This definition accepts interfaces.
export const objectKeys = Object.keys as <Type extends object>(value: Type) => Array<ObjectKeys<Type>>;

I would suggest changing objectEntries definition to "Type extends object" to allow passing interfaces, because I see no point in rejecting them, we are not making these calls safer by rejecting them (with or without allowing interfaces we are doing the same unsafe things https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript).

@aandrejevas aandrejevas changed the title Proposal: Strongly-typed version of Object.values() Proposal: Strongly-typed version of Object.values(). And synchronize objectKeys and objectEntries Aug 24, 2024
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

No branches or pull requests

1 participant