Skip to content

LocalStorage

LocalStorageMixin is a mixin function that allows you to create light-weight and type-safe services for accessing LocalStorage.

Interfaces

LocalStorageOptions<TLocalStorage>

Defined in: types.ts:5

The options for configuring a LocalStorage service.

Type Parameters

TLocalStorage

The type of your local storage data.

Properties

defaultValues

defaultValues: TLocalStorage

Defined in: types.ts:13

The set if default values to fall back to when reading data from window.localStorage.

key

key: string

Defined in: types.ts:9

The key the service should use when writing data to window.localStorage.

Functions

LocalStorageMixin()

LocalStorageMixin<TLocalStorage>(options): typeof LocalStorage

Defined in: local-storage.service-mixin.ts:24

A mixin that provides a standardized and type-safe abstraction of the browser’s localStorage API.

Type Parameters

TLocalStorage extends object

The type of your local storage data.

Parameters

options

LocalStorageOptions<TLocalStorage>

The LocalStorageOptions for configuring the service.

Returns

typeof LocalStorage

Your LocalStorage service class.

See

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Example

export interface ILocalStorage {
foo: string;
}
export class LocalStorage extends LocalStorageMixin<ILocalStorage>({
key: 'my-application',
defaultValues: { foo: 'bar' },
}) {}
// Write to local storage
LocalStorage.set('foo', 'baz');
// Read from local storage
const foo = LocalStorage.get('foo');