Skip to content
Alternative to lodash.get that makes it typed and cool as if optional typing proposal is there
Branch: master
Clone or download
Latest commit 217c5f6 Mar 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Prettier 🚧 Mar 13, 2019
.gitignore - Initial commit and v1.0.0 🎉🎉🎉 Mar 12, 2019
.prettierrc
.travis.yml
LICENSE Initial commit Mar 12, 2019
README.md - Removal of not needed undefined check 🚧 Mar 12, 2019
jest.config.js
package.json
tsconfig.json - Initial commit and v1.0.0 🎉🎉🎉 Mar 12, 2019
yarn.lock

README.md

ts-get

npm version Build Status codecov

Alternative to lodash.get that makes it typed and cool as if optional chaining proposal is there.

Means you're not only safely navigate object, but you're also getting 100% autocomplete and type-safeness 🎉

Usage and examples

import get from 'ts-get'

 type SomeType = {
  optionalField?: string
  nested?: {
    dangerousAccess?: string
  }
 } | undefined | null
 
 const input: SomeType = {}
 const input2: SomeType = {
    optionalField: "value",
 }
 
 get(input, it => it.optionalField, "default") // -> "default"
 get(input2, it => it.optionalField, "default") // -> "value"
 get(input2, it => it.nested.dangerousAccess, "default") // -> "default"
 get(input2, it => it.unknownField, "default") // -> Type error, `unknownField` doesn't exist on type
 get(input2, it => it.optionalField, 5) // -> Type error, third argument is not assignable to type `string`

Difference with lodash.get behavior

  • If your path gets null at the end, it will bail out to defaultValue or undefined. If you would like to get null returned anyway, just pass it as a defaultValue
  • If your type field is of type null and only null or undefined your field will be of type {}[]. I have no idea how to fix it 🤷‍♂️ PR Welcome 😇🙏
type A = {
  field: null | undefined// -> {}[] inside of the callback and as return type too
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.