1

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.

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! And thanks! This answer taught me about ReturnType which is an incredibly handy operator to use with any module that has a constructor which returns generic types!
Glad you found it useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.