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 KeyPath and PathValue types #158

Closed
wants to merge 6 commits into from
Closed

Add KeyPath and PathValue types #158

wants to merge 6 commits into from

Conversation

kainiedziela
Copy link
Contributor

Fixes #147

Playground link

KeyPath

Represents a dot path to a nested object property.

Use case: to be used in any helper function that extracts values based on a string dot path, like lodash.get or dot-prop.

PathValue

Targets a property from a nested object using a dot path.

Use case: Extracting a given value from a complex API requests that is otherwise non-essential outside that value.

Example

declare function get<T, P extends KeyPath<T>>(obj: T, path: P): PathValue<T, P>;
const user = {
  firstName: 'Foo',
  lastName: 'Bar',
  projects: [
    { name: 'Baz', contributors: 68 },
    { name: 'Waldo', contributors: 12 },
  ]
};

get(user, 'projects.0.name');

@sindresorhus
Copy link
Owner

I did you see #153 ?

@kainiedziela
Copy link
Contributor Author

I did not. Makes this obsolete I guess.

@sindresorhus
Copy link
Owner

Yeah, sorry. It was first so I'm going to go with that. Would be great if you could help review #153.

@kainiedziela kainiedziela deleted the key-path branch November 27, 2020 16:17
@mmkal mmkal mentioned this pull request Nov 27, 2020
5 tasks
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.

Proposal: Key path type
2 participants