Skip to content

Commit c263575

Browse files
committed
fix: relax persistence type contraints
1 parent 2ccb2e5 commit c263575

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

lib/persistence.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import { Dispatch, SetStateAction, useState } from 'react';
44

5-
type JsonPrimitive = string | number | boolean | null;
6-
type JsonArray = JsonValue[];
7-
type JsonObject = { [key: string]: JsonValue };
8-
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
9-
10-
function saveToLocalStorage<T extends JsonValue>(key: string, value: T): void {
5+
function saveToLocalStorage<T extends object>(key: string, value: T): void {
116
if (typeof localStorage === 'undefined') {
127
console.error('Local storage is not available.');
138
return;
@@ -25,7 +20,7 @@ function saveToLocalStorage<T extends JsonValue>(key: string, value: T): void {
2520
}
2621
}
2722

28-
function loadFromLocalStorage<T extends JsonValue>(key: string): T | undefined {
23+
function loadFromLocalStorage<T extends object>(key: string): T | undefined {
2924
if (typeof localStorage === 'undefined') {
3025
console.error('Local storage is not available.');
3126
return undefined;
@@ -44,7 +39,7 @@ function loadFromLocalStorage<T extends JsonValue>(key: string): T | undefined {
4439
}
4540
}
4641

47-
export function createLocalStorageInterface<T extends JsonValue>(
42+
export function createLocalStorageInterface<T extends object>(
4843
key: string,
4944
): { load: () => T | undefined; save: (value: T) => void } {
5045
return {
@@ -53,7 +48,7 @@ export function createLocalStorageInterface<T extends JsonValue>(
5348
};
5449
}
5550

56-
export function usePersistToLocalStorage<T extends JsonValue>(
51+
export function usePersistToLocalStorage<T extends object>(
5752
key: string,
5853
initialValue: T,
5954
): [T, Dispatch<SetStateAction<T>>] {

0 commit comments

Comments
 (0)