-
-
Notifications
You must be signed in to change notification settings - Fork 574
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 Get
type
#153
Add Get
type
#153
Conversation
Yes, we definitely want this. Thanks for working on it. We actually have an issue for it: #147 |
Make sure you adhere to https://github.com/sindresorhus/type-fest/blob/master/.github/contributing.md |
@sindresorhus thanks for reviewing, have updated based on comments. One note - in the latest revision I changed the get({ foo: 'bar' }, 'some.nonsense.string') // returns undefined, rather than throws Edit: hadn't seen the issue/playground - will take a look and see if there are any simplifications/improvements to be made. |
Can you add some more tests? And fix the lint errors. |
@mmkal This looks good to me when #153 (comment) is fixed. @voxpelli LGTY? |
Sounds good - will be able to work on it in a couple of days. Happy new year! |
Gets a deeply-nested property from an object, like lodash's
get
function.Use-case: retrieve a property from deep inside an API response or other complex object.
I couldn't find anything like this, so thought I'd put up a proposal since I've written shoddy versions of it a few times now.
This works by first splitting the path based on
.
and[...]
characters into a tuple of string keys.Then it recursively uses the head key to get the next property of the current object, until there are no keys
left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples
and dictionaries with number keys.
Todo:
likelynot blocked by Runtsd
intest
script #162 but I don't think tests are being run by CI properly right now so maybe)unknown
, notundefined
Numbers
toStringDigit
or similarFollow-ups:
KeyPath
type like in AddKeyPath
andPathValue
types #158Set
type -Set<{ foo: number }, 'bar.baz', string> = { foo: number; bar: { baz: string } }
foo\\.bar.baz
to allow for dots within keys, similar to dot-prop.Fixes #147