node-redis中的createClient()是什么类型

问题描述 投票:0回答:1

我正在将

await createClient()
传递给打字稿中的课程:

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 Cron {
  private redisClient: RedisClientType<Record<string, never>, Record<string, never>>

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

但是,我在

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'.

RedisClientType<Record<string, never>, Record<string, never>>
的解决方案来自这里

node-redis
1个回答
0
投票

我在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
© www.soinside.com 2019 - 2024. All rights reserved.