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

fix(object): the return type of objectMap should be the return type o… #46

Merged
merged 1 commit into from
Jun 25, 2024

Conversation

s3xysteak
Copy link
Contributor

Description

It fixed the return type issue of objectMap.

Before:

const val = objectMap({ one: 1, two: 2 }, (k, v) => [k, `${v}`]) // { one: '1', two: '2' }
// type of val will be `Record<string, number>`, but it actually is `Record<string, string>`

After:

const val = objectMap({ one: 1, two: 2 }, (k, v) => [k, `${v}`]) // { one: '1', two: '2' }
// type of val is `Record<string, string>`

Linked Issues

Fixes #45

Additional context

I have to assert the type of return value because it cannot be compatible with { [k: string]: NV; } and Record<NK, NV> and I have no idea how to fix it.

@antfu antfu merged commit e751695 into antfu:main Jun 25, 2024
3 checks passed
@@ -29,12 +29,12 @@ import type { DeepMerge } from './types'
* // { b: 2 }
* ```
*/
export function objectMap<K extends string, V, NK = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V> {
export function objectMap<K extends string, V, NK extends string | number | symbol = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<NK, NV> {
Copy link

@nandordudas nandordudas Jun 25, 2024

Choose a reason for hiding this comment

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

Just for curiosity, what do you think using of PropertyKey instead of string | number | symbol?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just for curiosity, what do you think using of PropertyKey instead of string | number | symbol?

This idea of yours is better I think. I use string | number | symbol just because that's what VSCode suggests.

Choose a reason for hiding this comment

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

Thanks for the honest answer.

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.

objectMap has bad return type
3 participants