Skip to content

THowland1/formik-path-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

formik-path-builder

A string builder that can build formik-readable paths of a given object in a TypeScript-enforced way

Example

import formikPathBuilder from 'formik-path-builder';

type Person = {
  name: {
    first: string;
    last: string;
  };
  friends: Person[];
};

const p = formikPathBuilder<Person>();
// or `const p = formikPathBuilder({} as Person);`

p('name')(); // = "name"
p('friends')(); // = "friends"
p('friends')(1)(); // = "friends.1"
p('friends')(1)('name')('first')(); // = "friends.1.name.first"
p('emailaddress')(); // throws compile-time error

TypeScript is super nifty

Output of string builder is the literal value of the exact string that will come out

const path = p('friends')(1)(); // = "friends.1"
type Path = typeof path; // = "friends.1"

In a loop (where the code is index-agnostic), the literal is preserved, only the unknown segments are generic

const person = {} as Person;

person.friends.forEach((_, i) => {
  const path = p('friends')(i)('name')(); // = "friends.0.name", or "friends.1.name", etc.
  type Path = typeof path; // = `friends.${number}.name`
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published