Show HN: I Made LocalStorage Better

3 months ago 1

local-storage-typed

TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published 12 minutes ago

A tiny, type-safe wrapper for localStorage powered by Zod. Validate and persist structured data in the browser with full type safety.


  • Schema-based storage with Zod
  • Type-safe reads and writes
  • ✅ Easy to use in React, Next.js, or vanilla JS
  • ✅ Small, dependency-light footprint

npm install local-storage-typed zod
import { z } from "zod"; import { LocalStorageTyped } from "local-storage-typed"; const store = new LocalStorageTyped({ user: z.object({ id: z.string(), name: z.string(), email: z.string().email(), }), theme: z.enum(["light", "dark"]), }); // Save data store.set("user", { id: "123", name: "Alice", email: "[email protected]", }); // Read data (typed!) const user = store.get("user"); console.log(user?.name); // "Alice" // Remove store.remove("user"); // Clear all schema-defined keys store.clear();

new LocalZodStore(schemas)

Create a new storage instance with your key–schema map.

store.set(key, value)

Validates and saves a value to localStorage.

store.get(key)

Retrieves and validates a value from localStorage.

store.remove(key)

Removes a single key from localStorage.

store.clear()

Removes all keys defined in the schema map.

Package Sidebar

Install

npm i local-storage-typed

Weekly Downloads

Last publish

12 minutes ago

Collaborators

  • nihaocami

Read Entire Article