forbidden
forbidden

Reputation: 31

What is the type of createClient() in node-redis

I am passing await createClient() to a class in typescript:

import { createClient } from "redis";
const redisClient = await createClient({ url: "redis://default:1234@redis:6379" })
  .on("error", (err) => console.log("Redis Client Error", err))
  .connect();

const cron = new Cron(redisClient);

Cron class:

class Cron {
  private redisClient: RedisClientType<Record<string, never>, Record<string, never>>

  constructor(redisClient: RedisClientType<Record<string, never>, Record<string, never>>
    ) {
    this.redisClient = redisClient;
  }
}

However, I am getting this error in Cron(redisClient):

Argument of type 'RedisClientType<{ graph: { CONFIG_GET: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); configGet: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); ... 15 more ...; slowLog: typeof import("/Users/tung/...' is not assignable to parameter of type 'RedisClientType<Record<string, never>, Record<string, never>>'.
  Type 'RedisClientType<{ graph: { CONFIG_GET: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); configGet: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); ... 15 more ...; slowLog: typeof import("/Users/tung/...' is not assignable to type 'RedisClient<Record<string, never>, Record<string, never>, Record<string, never>>'.
    Types of property 'options' are incompatible.
      Type 'RedisClientOptions<{ graph: { CONFIG_GET: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); configGet: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); ... 15 more ...; slowLog: typeof import("/Users/tu...' is not assignable to type 'RedisClientOptions<Record<string, never>, Record<string, never>, Record<string, never>>'.
        Type '{ graph: { CONFIG_GET: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); configGet: typeof import(".../node_modules/@redis/graph/dist/commands/CONFIG_GET"); ... 15 more ...; slowLog: typeof import("...' is not assignable to type 'Record<string, never>'.
          'string' index signatures are incompatible.
            Type 'RedisModule' is not assignable to type 'never'.

The solution of RedisClientType<Record<string, never>, Record<string, never>> is from here.

Upvotes: 1

Views: 163

Answers (1)

Guy Royse
Guy Royse

Reputation: 4332

I defined them like this in Redis OM:

/** A conventional Redis connection. */
export type RedisClientConnection = ReturnType<typeof createClient>

/** A clustered Redis connection. */
export type RedisClusterConnection = ReturnType<typeof createCluster>

/** A Redis connection, clustered or conventional. */
export type RedisConnection = RedisClientConnection | RedisClusterConnection

Upvotes: 2

Related Questions