An open API service indexing awesome lists of open source software.

https://github.com/katt/typed-form

typesafe formik example
https://github.com/katt/typed-form

Last synced: about 1 year ago
JSON representation

typesafe formik example

Awesome Lists containing this project

README

          

## Typed-safe Formik `` etc example

Example of using TypeScript 4.1 [Template Literal Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#template-literal-types) to give us a type safe usage of [Formik](https://formik.org/) ``s' `name`-attribute (& `useField` & ``).

![Code Example](code.gif)

> (Does not cover array fields)

## How the form component uses it

```tsx
// Set the initial values
const initialValues = {
name: "",
social: {
facebook: "",
twitter: "",
},
};

// Create type safe helper components
const { Field, ErrorMessage, useField } = typedFormik({
initialValues,
});

// in component:

```
### View code

- [`pages/index.tsx`](./pages/index.tsx)
- [`utils/typedFormik.tsx`](./utils/typedFormik.tsx)
- [`utils/types.ts`](./utils/types.ts)

### Play with it

```bash
git clone git@github.com:KATT/typed-form.git cd typed-form && yarn && yarn dev
```

---

Created by [@alexdotjs](https://twitter.com/alexdotjs). Credits to [Craig](https://twitter.com/phenomnominal) who wrote [this article](https://dev.to/phenomnominal/i-need-to-learn-about-typescript-template-literal-types-51po).