1.0.2 • Published 2 years ago

ts-field-paths v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Install

yarn add -D ts-field-paths

Requirements

This library uses Template Literal Types, so TypeScript 4.1<= is required

🐇 Quick start

    import { FieldPath } from 'ts-field-paths'

    interface IChildren {
      name: string
      age: number
    }
    enum ResultEnum {
      Success = 'success',
      Failure = 'failure',
    }
    interface IPerson {
      name: string
      age: number
      adress: {
        city: string
        street: string
      }
      children: IChildren[]
      nestedChildren: IChildren[][]
      extraNestedChildren: IChildren[][][]
      favoriteNames: ['Stepan', 'Karina']
      anyArrData: string[]
      result: ResultEnum
    }

    const index = 0
    const getPersonProperty = (path: FieldPath<IPerson>) => {
      // used for example in lodash get _.get(path)
    }

    getPersonProperty('adress.city')
    getPersonProperty(`children[${index}].name`)
    getPersonProperty('anyArrData[0]')
    getPersonProperty('nestedChildren[0][0]')
    getPersonProperty('extraNestedChildren[0][0][0].name')
    getPersonProperty('adress')

✨ Awesome autocomplete

💎 Strongly typed