A wrapper to provide type safe access to the localStorage and sessionStorage.
npm install typed-local-store
# or
yarn add typed-local-store
Create a schema of your desired storage structure:
interface Schema {
counter: number;
message: string;
user: {
id: string;
name: string;
email: string;
isAdmin: boolean;
};
posts: string[];
}
Then create your storage using the defined schema:
import TypedLocalStore from 'typed-local-store';
const typedStorage = new TypedLocalStore<Schema>();
The constructor can receive a options object to configure the store.
Property | Required | Default | Description |
---|---|---|---|
storage | false | "localStorage" | Choose the storage type, "localStorage" or "sessionStorage" |
The API of typed-local-store mimics the Storage API except for one exception, the Storage APIs length property is implemented as a method.