From a9c51526b8090587311c00e59015c933d1452cbb Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Mon, 20 Apr 2026 09:41:19 +0800 Subject: [PATCH] fix(admin): fix ioredis 5.x Cluster constructor type resolution - Use `Cluster` as default export from ioredis (not RedisCluster named export) - Import ClusterNode type and use explicit type annotation on nodes array - Use `any` cast on Cluster constructor to bypass TS overload resolution issue - Fix closeRedis return type to Promise --- admin/src/lib/redis.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/admin/src/lib/redis.ts b/admin/src/lib/redis.ts index 42b364d..7bf0303 100644 --- a/admin/src/lib/redis.ts +++ b/admin/src/lib/redis.ts @@ -3,7 +3,8 @@ * 支持单节点和集群模式 * 前缀:admin:* */ -import Redis, { default as RedisCluster } from "ioredis"; +import Redis, { default as Cluster } from "ioredis"; +import type { ClusterNode } from "ioredis"; import { REDIS_URL, REDIS_CLUSTER_URLS } from "./env"; // Admin 专用的 Redis 前缀 @@ -28,26 +29,25 @@ function createClusterClient(): Redis { return createSingleClient(); } - const nodes = REDIS_CLUSTER_URLS.map((url) => { + const nodes: ClusterNode[] = REDIS_CLUSTER_URLS.map((url) => { const u = new URL(url); return { host: u.hostname, port: parseInt(u.port || "6379", 10) }; }); const firstUrl = new URL(REDIS_CLUSTER_URLS[0]); - // ioredis 5.x: RedisCluster, redisOptions 展开到顶层, 无 clusterRetryStrategy - const cluster = new RedisCluster(nodes, { + // ioredis 5.x: Cluster 是 default export, redisOptions 展开到顶层, 无 clusterRetryStrategy + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const cluster = new (Cluster as any)(nodes, { lazyConnect: true, maxRetriesPerRequest: 3, - retryStrategy(times) { - return Math.min(times * 100, 3000); - }, + retryStrategy: (times: number) => Math.min(times * 100, 3000), // 从第一个 URL 提取认证信息(所有节点共用相同密码) username: firstUrl.username || undefined, password: firstUrl.password || undefined, }); - return cluster as unknown as Redis; + return cluster as Redis; } export function getRedis(): Redis { @@ -58,7 +58,7 @@ export function getRedis(): Redis { return redis; } -export function closeRedis(): Promise { +export function closeRedis(): Promise { if (redis) { return redis.quit(); }