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

Add new type: ValuesType #75

Closed
piotrwitek opened this issue Apr 23, 2019 · 10 comments · Fixed by #99
Closed

Add new type: ValuesType #75

piotrwitek opened this issue Apr 23, 2019 · 10 comments · Fixed by #99

Comments

@piotrwitek
Copy link
Owner

piotrwitek commented Apr 23, 2019

Issuehunt badges

Proposal to add ValuesType, looking for feedback:

export type ValuesType<T extends ArrayLike[] | Record<string, any>> = T extends ArrayLike[]
  ? T[number]
  : T extends object
  ? T[keyof T]
  : never;

const arr = [1, 2];
const obj = { key: 'value' };

type t1 = typeof arr[number];
type t2 = typeof obj[keyof typeof obj]; // this is really mouthful and I was annoyed by this many times

// consistent and simple
type t3 = ValuesType<typeof arr>; // number
type t4 = ValuesType<typeof obj>; // string

IssueHunt Summary

axure axure has been rewarded.

Backers (Total: $25.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@levenleven
Copy link
Contributor

I think { [K in keyof T]: T[K] extends any ? T[K] : never }[keyof T] part can be simplified to T[keyof T]

@piotrwitek
Copy link
Owner Author

@levenleven agreed! I corrected the proposal, thanks!

@levenleven
Copy link
Contributor

Another suggestion regarding constraints. I find them a bit restrictive

  1. any [] - maybe it is enough to just require ArrayLike<any> so readonly arrays will also work
  2. Record<string, any> - maybe just object? Is it needed at all?

@piotrwitek
Copy link
Owner Author

piotrwitek commented Apr 25, 2019

@levenleven As far as I tested any[] works with readonly arrays
Record<string, any> is much better than object, but optimal would be Record<any, any> - it allows for better constrains in conditional types, so I use them as default

@IssueHuntBot
Copy link

@IssueHunt has funded $25.00 to this issue.


@levenleven
Copy link
Contributor

Here's an example where it doesn't work with readonly arrays:

const arr = [1, 2] as const;

See result of ValuesType<typeof arr>
Playground

@piotrwitek
Copy link
Owner Author

piotrwitek commented Apr 29, 2019

Thanks for the example now I understand 👍 So the problem here is it's a tuple, not a read-only array, which needs a special case treatment a length property which ArrayLike implements.

Yeah, I agree we should go with ArrayLike condition.

Axure added a commit to Axure/utility-types that referenced this issue Sep 8, 2019
@Axure Axure mentioned this issue Sep 8, 2019
6 tasks
Axure added a commit to Axure/utility-types that referenced this issue Sep 8, 2019
feat(utility-types): Add `ValuesType`.

Proposed in piotrwitek#75.
@issuehunt-oss
Copy link

issuehunt-oss bot commented Sep 21, 2019

@piotrwitek has rewarded $17.50 to @Axure. See it on IssueHunt

  • 💰 Total deposit: $25.00
  • 🎉 Repository reward(20%): $5.00
  • 🔧 Service fee(10%): $2.50

@drewlustro
Copy link

I could be misunderstanding this utility type, but it doesn't look like it works for interfaces:

interface Foo {
  bar: string;
  baz: number;
}

type FooValue = ValuesType<Foo>;
// expected: string | number
// received: any

@piotrwitek
Copy link
Owner Author

@drewlustro actually I have tested and it works.
Zrzut ekranu 2020-02-26 o 12 28 06 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants