I am currently using cache manager module from NestJS and I was wondering why I can't get the NodeRedis client like this :
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {
cacheManager.store.getClient();
}
I am getting this error :
ERROR in [...].controller.ts:24:24
TS2339: Property 'getClient' does not exist on type 'Store'.
22 | @Inject(CACHE_MANAGER) private cacheManager: Cache,
23 | ) {
> 24 | cacheManager.store.getClient();
| ^^^^^^^^^
25 | }
I did configured the cache-manager-redis-store when I registered the CacheModule then I supposed I could get the client.
tl;dr It seems that
cache-manager-redis-storeisn't properly supported by TypeScript because theRedisCachetype is private and can't be imported.As a workaround, you could copy the private types to your own file:
Deep Dive
It looks like the
CACHE_MANAGERprovided by NestJS is created by createCacheManager, which imports the npm packagecache-managerand then invokes itscachingfunction with the store package you give.I think the
Cachetype you're using is imported fromcache-manager. The type is defined here and contains aStorecontained here in the same file.getClientis not a method method on that interface, so the error message is correct.However, since you're using an external package for the store, there's more to it than
caching-managerknows about. Looking at the types forcache-manager-redis-store, you can see there's a type RedisStore that extendsStoreand includesgetClient.So
cacheManagertechnically hasgetClientsince you've configured it with the redis store package, but you need to set the type on yourcacheManagervariable toRedisCachefor TypeScript to allow it.Based on the DefinitelyTyped types for
cache-manager-redis-store, it seems your type should beredisStore.CacheManagerRedisStore.RedisCacheif you imported the package asredisStore, but there seems to be an issue because that namespaceCacheManagerRedisStoreis not exported.There's an issue about this same problem on the repo and there's an issue asking for TypeScript support. It seems this package isn't properly supported by TypeScript at this time.