Skip to content

sajmoni/typed-ls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typed-ls

Type-safe local storage

✨ Features

A tiny helper library that creates type-safe get and set functions for working with local storage

Why?

  • The key is only defined once
  • Ensures the same type is used when both getting and setting the value
  • Reduces boilerplate

How to use

import { createStoredValue } from 'typed-ls'

const defaultValue = 'en'

export const language = createStoredValue('language', defaultValue)
// language.get() => 'en'
// language.set('jp')
// language.remove()

The type will be inferred from the default value. If this is not possible (for example if the default value is undefined or []) you can explicitly set the type instead:

export const language = createStoredValue<string[]>('languages', [])

📰 API

createStoredValue<T>(key: string, defaultPayload: T): StoredValue
type StoredValue = {
  get: () => T
  set: (payload: T) => void
  remove: () => void
}

key

The local storage key

defaultPayload

If there is no value in local storage, get will return the defaultPayload instead


📦 Install

npm install typed-ls

Local storage not available

If local storage is not available then:

  • get always returns default values
  • set and remove are no-ops

This can happen if the user has turned off local storage in the privacy setting of their browser