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

Feature request: KeysMatching (filtered keyof) #942

Open
vincerubinetti opened this issue Aug 22, 2024 · 4 comments
Open

Feature request: KeysMatching (filtered keyof) #942

vincerubinetti opened this issue Aug 22, 2024 · 4 comments

Comments

@vincerubinetti
Copy link

vincerubinetti commented Aug 22, 2024

type KeysMatching<Obj, Type> = {
  [Key in keyof Obj]-?: Key extends Type ? Key : never;
}[keyof Obj];

Quick way to get keys of an object matching a particular patterns, e.g. template literal types. Could be useful for composing with other util types like ValueOf.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

Could you include an example use-case (code)?

@vincerubinetti
Copy link
Author

vincerubinetti commented Aug 22, 2024

const data = {
  property: "123";
  project: "abc";
  topRegions: SomeComplexTypeA;
  topContinents: SomeComplexTypeB;
  topCountries: SomeComplexTypeC;
}
type Data = typeof data;
type Top = ValueOf<Data, KeysMatching<Data, `top${string}`>>;
// SomeComplexTypeA | SomeComplexTypeB | SomeComplexTypeC

@vincerubinetti
Copy link
Author

I could do a PR if you want

@fregante fregante changed the title Feature request: KeysMatching Feature request: KeysMatching (filtered keyof) Aug 22, 2024
@Emiyaaaaa
Copy link
Collaborator

I perfer split this to two type: ObjectKeys and Match(or other name). These both are useful type, and both work well in combination with other types.

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

No branches or pull requests

4 participants