Skip to content

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

License

Notifications You must be signed in to change notification settings

sowtame/ts-field-paths

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published