22
33import { 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