I am attempting to enhance the window.localStorage
object by adding custom methods and returning an object in the form of EnhancedStorageType
, which includes extra members.
Prior to using the spread operator, the storage.clear
method is clearly defined... however, it seems to get lost right after the spread operation.
type EnhancedStorageType = Storage & {
getAllItems: () => { [key: string]: any },
};
export const useEnhancedStorage() => EnhancedStorageType {
const storage = window.localStorage;
console.log(storage.clear); // This works fine here: ƒ clear() { [native code] }
const store = {
...storage,
getItems(...keys: string[]) {
return keys.map(key => this.getItem(key));
},
};
console.log(store.clear); // This fails here: undefined
return store;
}