Skip to Content
Introduction

parsistent

Typed localStorage, sessionStorage and cookies, shaped like the browser APIs.

import { createStorage, number, oneOf } from 'parsistent'; const local = createStorage('local', { theme: oneOf(['light', 'dark'], 'light'), count: number(0), }); local.getItem('count'); // number, never a raw string local.setItem('count', 3);

Try it: this demo uses parsistent itself.

 
Reload the page, or open it in a second tab: the values stay and sync.

Why parsistent?

The Web Storage and Cookie Store APIs only store strings, and anything can be in them: values written by an older version of your app, edited in DevTools, or set by an extension. parsistent keeps the API you already know, and adds what it lacks:

localStorageparsistent
Valuesstringstyped (number, Date, unions, JSON…)
Missing or invalid valuenull, or garbageyour default
Validationnonebuilt in, or any Zod / Valibot / ArkType
Keysany stringonly the keys you declared, type-checked
Changes in the same tabno eventchange event
Changes in other tabsstorage event, raw stringschange event, parsed values
Server renderinglocalStorage is not defineddefaults on the server, cookies from headers
React—useItem(), like useState

No runtime dependencies. React is optional.

Last updated on