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: add Paths type #213

Closed
zhaoyao91 opened this issue May 20, 2021 · 3 comments · Fixed by #741
Closed

Feature Request: add Paths type #213

zhaoyao91 opened this issue May 20, 2021 · 3 comments · Fixed by #741
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@zhaoyao91
Copy link

zhaoyao91 commented May 20, 2021

//  by @金鑫

type Ensure<X, Y> = X extends Y ? X : never;
type SafeKey<T> = T extends string | number ? `${T}` : never;
type SafeValue<O, K> = K extends keyof O ? O[K] : never;

type Paths<O, P = ""> = O extends object
  ? {
      [K in keyof O]:
        | (P extends "" ? K : `${SafeKey<P>}.${SafeKey<K>}`)
        | Paths<O[K], P extends "" ? K : `${SafeKey<P>}.${SafeKey<K>}`>;
    }[keyof O]
  : never;

type Get<O, P extends Paths<O>> = P extends `${infer X}.${infer XS}`
  ? Get<SafeValue<O, X>, Ensure<XS, Paths<SafeValue<O, X>>>>
  : SafeValue<O, P>;

These types help us generate all possible paths of an object, which help build library like lodash.get and could be considered as companions of the already exists Get.

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
@zhaoyao91 zhaoyao91 changed the title Feature Request: add Paths and Leaves types Feature Request: add Paths type May 20, 2021
@LinusU LinusU mentioned this issue Oct 22, 2021
Repository owner deleted a comment from Beraliv Dec 7, 2021
@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Mar 22, 2022
@sindresorhus
Copy link
Owner

If anyone wants to work on this, see the initial attempt in #300.

Copy link
Owner

Thank you @Emiyaaaaa for contributing to close this issue! ⭐

The rewards from this issue, totalling $100, has been shared with you.

What now?

  1. Create a Polar account
  2. See incoming rewards & setup Stripe to receive them
  3. Get payouts as backers finalize their payments

If you already have a Polar account setup, you don't need to do anything.

@Emiyaaaaa
Copy link
Collaborator

Thank you @Emiyaaaaa for contributing to close this issue! ⭐

The rewards from this issue, totalling $100, has been shared with you.

What now?

  1. Create a Polar account
  2. See incoming rewards & setup Stripe to receive them
  3. Get payouts as backers finalize their payments

If you already have a Polar account setup, you don't need to do anything.

Wow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants