typed-local-storage-ttl
v1.0.1
Published
Type-safe wrapper for localStorage with expiry TTL and automatic compression.
Maintainers
Readme
typed-local-storage-ttl
Why use typed-local-storage-ttl? HTML5 localStorage is string-only, lack structure typing, and does not support expiry times (TTL). Outdated configuration and session data remains in localstorage indefinitely until cleared.
A wrapper for browser localStorage that provides full TypeScript type safety, automatic JSON serialization, and duration TTL expiration options.
⚡ Features
- Full TypeScript type safety for keys and values
- Automatic JSON parsing and stringify mapping
- Expiration times (TTL) with automatic removal on read
- Failsafe fallback for server-side rendering (SSR)
📦 Installation
npm i typed-local-storage-ttl🚀 Usage
import { TypedLocalStorage } from 'typed-local-storage-ttl';
interface UserSession {
token: string;
name: string;
}
const storage = new TypedLocalStorage<UserSession>('user-session');
// Save with a 5-minute TTL (300000ms)
storage.set({ token: 'abc', name: 'Halonic' }, 300000);
// Get value
const session = storage.get(); // Returns null if expired
console.log(session);⚙️ API Reference
class TypedLocalStorage
new TypedLocalStorage(keyName)set(value, ttlMs?): Save value.get(): Retrieve value, returnsnullif expired.remove(): Remove key.
📺 Demonstration

📄 License
MIT License.
