ts-field-paths
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Ts-field-paths

Ts-field-paths is a library for strongly typing an object's property path, including nested properties and even arrays.

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

autocomplete screenshot

💎 Strongly typed

strongly typed screenshot

Package Sidebar

Install

npm i ts-field-paths

Weekly Downloads

22

Version

1.0.2

License

MIT

Unpacked Size

151 kB

Total Files

11

Last publish

Collaborators

  • sowtame