Skip to content

Storage Unified Local Storage

Introduction

localStorage and sessionStorage encapsulate set, get, remove, clear and other methods for convenient use.

  • set Set value
  • get Get value
  • remove Delete value
  • clear Clear values

⚠️ Ensure type-safe key access

  • The generic T passed to local extends Local
  • The generic T passed to session extends Session
  • Local and Session are defined in src/types/global.d.ts

Usage Example

Add custom types to Local and Session in src/types/global.d.ts

ts
/** LocalStorage */
interface Local {
  // Existing types ...
  localMsg: string; 
}

/** SessionStorage */
interface Session {
  // Existing types ...
  sessionMsg: string; 
}

LocalStorage

ts
import { local } from "@/utils";

/**
 * Incorrect usage
 * - The parameter "msg" cannot be assigned to a parameter of type "keyof Local"
 */
local.set("msg", "hello World"); 

/**
 * Correct usage
 */
local.set("localMsg", "hello VitePress");
local.get("localMsg");
local.remove("localMsg");
local.clear();

SessionStorage

ts
import { session } from "@/utils";

/**
 * Incorrect usage
 * - The parameter "msg" cannot be assigned to a parameter of type "keyof Session"
 */
session.set("msg", "hello World"); 

/**
 * Correct usage
 */
session.set("sessionMsg", "hello VitePress");
session.get("sessionMsg");
session.remove("sessionMsg");
session.clear();

Contributors

Changelog

Released under the MIT License