npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

type-localstorage

v1.0.2

Published

A simple and powerful **type-safe wrapper** around `localStorage` for TypeScript projects.

Readme

typed-localstorage

A simple and powerful type-safe wrapper around localStorage for TypeScript projects.

✅ What This Library Does

const testStorage = createTypedStorage<TestData>('test-storage', {
  prefix: 'demo',
  defaultValue: {
    name: 'Default User',
    count: 0,
    lastUpdated: new Date().toISOString()
  }
});

This creates a typed localStorage interface with:

✅ Key Features

1. Type Safety

You define the shape of your data:

interface TestData {
  name: string;
  count: number;
  lastUpdated: string;
}

Now your get() and set() are fully typed. Avoid bugs by ensuring you only store valid data.

2. Prefixing

prefix: 'demo'

Ensures all keys are stored as demo-test-storage, helping avoid naming collisions with other data.

3. Default Value

If there's no value in localStorage, a default is returned automatically.

defaultValue: { ... }

4. Cross-Tab Syncing

testStorage.onChange((newValue) => { ... })

Keeps all open tabs in sync when one tab updates the data — perfect for modern web apps.

5. Built-in Serialization

No need to manually JSON.stringify or JSON.parse. This library does it for you.


🧠 Why This Library Is Useful

| Feature | Benefit | |------------------------|-------------------------------------------------------------------------| | 🔐 Type-Safety | Prevents bugs, improves dev experience | | 🏷️ Prefix Support | Avoids key collisions in shared environments | | 🧱 Default Values | Guarantees get() always returns a valid value | | 🔄 Cross-tab Sync | Enables real-time synchronization across multiple tabs | | 🔁 Built-in Serialization | Clean code: handles parsing/stringifying for you |


🟢 Real-World Use Cases

  • Persisting UI preferences (theme, layout, language)
  • Saving form progress across sessions
  • Tracking counters or stats
  • Syncing state across multiple open tabs
  • Building dashboards or SPAs with persistent state

🚀 Example Usage (React)

import React, { useEffect, useState } from 'react';
import { createTypedStorage } from 'type-localstorage';

interface TestData {
  name: string;
  count: number;
  lastUpdated: string;
}

function App() {
  const testStorage = createTypedStorage<TestData>('test-storage', {
    prefix: 'demo',
    defaultValue: {
      name: 'Default User',
      count: 0,
      lastUpdated: new Date().toISOString()
    }
  });

  const [data, setData] = useState<TestData>(testStorage.get()!);

  useEffect(() => {
    const unsubscribe = testStorage.onChange((newValue) => {
      if (newValue) setData(newValue);
    });
    return () => unsubscribe();
  }, []);

  const handleIncrement = () => {
    const newData = {
      ...data,
      count: data.count + 1,
      lastUpdated: new Date().toISOString()
    };
    testStorage.set(newData);
    setData(newData);
  };

  const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const newData = {
      ...data,
      name: e.target.value,
      lastUpdated: new Date().toISOString()
    };
    testStorage.set(newData);
    setData(newData);
  };

  return (
    <div style={{ padding: '20px' }}>
      <h1>Type-Safe LocalStorage Demo</h1>

      <div style={{ marginBottom: '20px' }}>
        <label>
          Name:
          <input
            type="text"
            value={data.name}
            onChange={handleNameChange}
            style={{ marginLeft: '10px' }}
          />
        </label>
      </div>

      <div style={{ marginBottom: '20px' }}>
        <button onClick={handleIncrement}>
          Increment Count: {data.count}
        </button>
      </div>

      <div>
        <strong>Last Updated:</strong> {new Date(data.lastUpdated).toLocaleString()}
      </div>

      <div style={{ marginTop: '20px', color: 'gray' }}>
        <small>Open this page in multiple tabs to test cross-tab synchronization!</small>
      </div>
    </div>
  );
}

export default App;

📦 Install

npm install type-localstorage

🧪 Contribute

We welcome improvements, new features, and bug fixes. Open an issue or a pull request!


📝 License

MIT License