🗃️ Type-Safe Web Storage API Wrapper
npm i @jill64/typed-storage
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()