Skip to content

enhance: Object.value|entries supports a generic Record type #3

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

Merged
merged 1 commit into from
Jun 28, 2021

Conversation

hachibeeDI
Copy link
Contributor

Overview

Add type support for Record<K, V> like below:

function x<K extends string, V>(arg: Record<K, V>): readonly V[] {
  return Object.values(arg);
}

Expected result

Works 👌

Actual result

Type 'unknown[] | Record<K, V>[K & (string | number | symbol)][] | Record<K, V>[K & string][] | Record<K, V>[K & number][]' is not assignable to type 'readonly V[]'.

Note

I tried to add a conditional type infer for that but doesn't work. :(

  T extends Record<any, infer V> 
    ? V[]
    : string | number extends keyof T
    ? T[string | number][]
    : string extends keyof T
    ? T[string][]
    : number extends keyof T
    ? T[number][]
    : unknown[];

So I use overload to fix this issue.

Copy link
Owner

@uhyo uhyo left a comment

Choose a reason for hiding this comment

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

Sorry for very late review.
LGTM.
Thank you very much!

@uhyo uhyo merged commit 968cc8f into uhyo:master Jun 28, 2021
@hachibeeDI hachibeeDI deleted the enhance/object-support-generic-record branch September 28, 2021 07:38
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.

2 participants