apollo-cache-persist: typescript incompatible type with window.localStorage

I am getting an error when using typescript that says the localStorage type is not compatible. I have searched the issues and found #17 but that seems to be unrelated as far as I can tell.

here is what I am doing

const persistor = new CachePersistor({
  cache,
  storage: window.localStorage,
  debug: true,
  });

and here is the error I am getting

Argument of type '{ cache: InMemoryCache; storage: Storage; debug: true; }' is not assignable to parameter of type 'ApolloPersistOptions<NormalizedCacheObject>'. Types of property 'storage' are incompatible. Type 'Storage' is not assignable to type 'PersistentStorage<PersistedData<NormalizedCacheObject>>'. Types of property 'setItem' are incompatible. Type '(key: string, value: string) => void' is not assignable to type '(key: string, data: PersistedData<NormalizedCacheObject>) => void | Promise<void>'. Types of parameters 'value' and 'data' are incompatible. Type 'PersistedData<NormalizedCacheObject>' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Just ran into the same problem, Typecasting the localStorage solved it for me.

persistCache({
  cache,
  storage: window.localStorage as PersistentStorage<PersistedData<NormalizedCacheObject>>,
})

@batamire you’ll have to import the required types:

import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory';
import { PersistentStorage, PersistedData } from 'apollo-cache-persist/types';

@wodCZ changes look good. Ping me again if you need final check/run all examples storages etc.