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

3.1.5 • 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

See ts-serde for more information on type guard

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

6,167

Version

3.1.5

License

MIT

Unpacked Size

6.77 kB

Total Files

15

Last publish

Collaborators

  • jill64