@jill64/typed-storage
TypeScript icon, indicating that this package has built-in type declarations

2.2.3 • Public • Published

@jill64/typed-storage

npm-version npm-license npm-download-month npm-min-size ci.yml

🗃️ Type-Safe Web Storage API Wrapper

Installation

npm i @jill64/typed-storage

Example

import { typedStorage } from '@jill64/typed-storage'
import { json } from '@jill64/typed-storage/serde'

const key = 'localStorageKey'
const value = ['value1', 'value2', 'value3']

const guard = (x: unknown): x is string[] =>
  Array.isArray(x) && x.every((y) => typeof y === 'string')

const store = typedStorage(key, json(guard, []), {
  // Optional
  // Use sessionStorage
  // sessionStorage?: boolean
})

// string[]
const storedValue = store.get()

store.set(value)

const unsubscriber = store.subscribe((newValue) => {
  // called when localStorage value changes
  console.log(newValue)
})

// unsubscribe
unsubscriber()

License

MIT

Package Sidebar

Install

npm i @jill64/typed-storage

Weekly Downloads

37,237

Version

2.2.3

License

MIT

Unpacked Size

7.18 kB

Total Files

17

Last publish

Collaborators

  • jill64