From b97d18b61071b2d615c4606ee6b5930854b9e4e4 Mon Sep 17 00:00:00 2001 From: leibale Date: Thu, 23 Dec 2021 17:17:19 -0500 Subject: [PATCH] fix some types --- README.md | 6 ++++- index.ts | 4 ++-- packages/client/lib/client/index.spec.ts | 4 ++-- packages/client/lib/client/index.ts | 24 ++++++++++++-------- packages/client/lib/client/multi-command.ts | 11 +++++---- packages/client/lib/cluster/index.ts | 6 ++--- packages/client/lib/cluster/multi-command.ts | 9 ++++---- packages/client/lib/command-options.ts | 2 +- packages/client/lib/commander.ts | 2 +- packages/test-utils/lib/index.ts | 8 +++---- 10 files changed, 42 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index b38aadf036..911112ac5a 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,11 @@ This pattern works especially well for blocking commands—such as `BLPOP` and ` ```typescript import { commandOptions } from 'redis'; -const blPopPromise = client.blPop(commandOptions({ isolated: true }), 'key', 0); +const blPopPromise = client.blPop( + commandOptions({ isolated: true }), + 'key', + 0 +); await client.lPush('key', ['1', '2']); diff --git a/index.ts b/index.ts index 2875c6fd84..18b6d4a191 100644 --- a/index.ts +++ b/index.ts @@ -15,7 +15,7 @@ const modules = { ts: RedisTimeSeries }; -export function createClient>( +export function createClient( options?: Omit, 'modules'> ): RedisClientType { return _createClient({ @@ -24,7 +24,7 @@ export function createClient>( }); } -export function createCluster>( +export function createCluster( options: Omit, 'modules'> ): RedisClusterType { return _createCluster({ diff --git a/packages/client/lib/client/index.spec.ts b/packages/client/lib/client/index.spec.ts index 05c0deb897..bf9fa7ae3e 100644 --- a/packages/client/lib/client/index.spec.ts +++ b/packages/client/lib/client/index.spec.ts @@ -573,8 +573,8 @@ describe('Client', () => { describe('PubSub', () => { testUtils.testWithClient('should be able to publish and subscribe to messages', async publisher => { function assertStringListener(message: string, channel: string) { - assert.ok(typeof message === 'string'); - assert.ok(typeof channel === 'string'); + assert.equal(typeof message, 'string'); + assert.equal(typeof channel, 'string'); } function assertBufferListener(message: Buffer, channel: Buffer) { diff --git a/packages/client/lib/client/index.ts b/packages/client/lib/client/index.ts index 9e1b2d37cc..2b801a5408 100644 --- a/packages/client/lib/client/index.ts +++ b/packages/client/lib/client/index.ts @@ -34,9 +34,11 @@ type ConvertArgumentType = ) : ( Type extends Set ? Set> : ( Type extends Map ? Map> : ( - Type extends Record ? { - [Property in keyof Type]: ConvertArgumentType - } : Type + Type extends Array ? Array> : ( + Type extends Record ? { + [Property in keyof Type]: ConvertArgumentType + } : Type + ) ) ) ); @@ -57,21 +59,23 @@ type WithCommands = { [P in keyof typeof COMMANDS]: RedisClientCommandSignature<(typeof COMMANDS)[P]>; }; +export type ExcludeMappedString = string extends S ? never : S; + export type WithModules = { - [P in keyof M as M[P] extends never ? never : P]: { - [C in keyof M[P]]: RedisClientCommandSignature; + [P in keyof M as ExcludeMappedString

]: { + [C in keyof M[P] as ExcludeMappedString]: RedisClientCommandSignature; }; }; export type WithScripts = { - [P in keyof S as S[P] extends never ? never : P]: RedisClientCommandSignature; + [P in keyof S as ExcludeMappedString

]: RedisClientCommandSignature; }; -export type RedisClientType, S extends RedisScripts = Record> = +export type RedisClientType = RedisClient & WithCommands & WithModules & WithScripts; export type InstantiableRedisClient = - new (...args: ConstructorParameters) => RedisClientType; + new (options?: RedisClientOptions) => RedisClientType; export interface ClientCommandOptions extends QueueCommandOptions { isolated?: boolean; @@ -85,7 +89,7 @@ export default class RedisClient return commandOptions(options); } - static extend, S extends RedisScripts = Record>(plugins?: RedisPlugins): InstantiableRedisClient { + static extend(plugins?: RedisPlugins): InstantiableRedisClient { const Client = extendWithModulesAndScripts({ BaseClass: RedisClient, modules: plugins?.modules, @@ -101,7 +105,7 @@ export default class RedisClient return Client; } - static create, S extends RedisScripts = Record>(options?: RedisClientOptions): RedisClientType { + static create(options?: RedisClientOptions): RedisClientType { return new (RedisClient.extend(options))(options); } diff --git a/packages/client/lib/client/multi-command.ts b/packages/client/lib/client/multi-command.ts index d45ac6ce31..5d69b93315 100644 --- a/packages/client/lib/client/multi-command.ts +++ b/packages/client/lib/client/multi-command.ts @@ -2,25 +2,26 @@ import COMMANDS from './commands'; import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands'; import RedisMultiCommand, { RedisMultiQueuedCommand } from '../multi-command'; import { extendWithCommands, extendWithModulesAndScripts, LegacyCommandArguments, transformLegacyCommandArguments } from '../commander'; +import { ExcludeMappedString } from '.'; type RedisClientMultiCommandSignature = (...args: Parameters) => RedisClientMultiCommandType; type WithCommands = { - [P in keyof typeof COMMANDS]: RedisClientMultiCommandSignature<(typeof COMMANDS)[P], M, S> + [P in keyof typeof COMMANDS]: RedisClientMultiCommandSignature<(typeof COMMANDS)[P], M, S>; }; type WithModules = { - [P in keyof M as M[P] extends never ? never : P]: { - [C in keyof M[P]]: RedisClientMultiCommandSignature; + [P in keyof M as ExcludeMappedString

]: { + [C in keyof M[P] as ExcludeMappedString]: RedisClientMultiCommandSignature; }; }; type WithScripts = { - [P in keyof S as S[P] extends never ? never : P]: RedisClientMultiCommandSignature + [P in keyof S as ExcludeMappedString

]: RedisClientMultiCommandSignature; }; -export type RedisClientMultiCommandType, S extends RedisScripts = Record> = +export type RedisClientMultiCommandType = RedisClientMultiCommand & WithCommands & WithModules & WithScripts; export type RedisClientMultiExecutor = (queue: Array, chainId?: symbol) => Promise>; diff --git a/packages/client/lib/cluster/index.ts b/packages/client/lib/cluster/index.ts index 0a9ead636a..157b607b08 100644 --- a/packages/client/lib/cluster/index.ts +++ b/packages/client/lib/cluster/index.ts @@ -20,10 +20,10 @@ type WithCommands = { [P in keyof typeof COMMANDS]: RedisClientCommandSignature<(typeof COMMANDS)[P]>; }; -export type RedisClusterType, S extends RedisScripts = Record> = +export type RedisClusterType = RedisCluster & WithCommands & WithModules & WithScripts; -export default class RedisCluster, S extends RedisScripts = Record> extends EventEmitter { +export default class RedisCluster extends EventEmitter { static extractFirstKey(command: RedisCommand, originalArgs: Array, redisArgs: RedisCommandArguments): RedisCommandArgument | undefined { if (command.FIRST_KEY_INDEX === undefined) { return undefined; @@ -34,7 +34,7 @@ export default class RedisCluster return command.FIRST_KEY_INDEX(...originalArgs); } - static create, S extends RedisScripts = Record>(options?: RedisClusterOptions): RedisClusterType { + static create(options?: RedisClusterOptions): RedisClusterType { return new (extendWithModulesAndScripts({ BaseClass: RedisCluster, modules: options?.modules, diff --git a/packages/client/lib/cluster/multi-command.ts b/packages/client/lib/cluster/multi-command.ts index 974dcb1029..a7b6f33f92 100644 --- a/packages/client/lib/cluster/multi-command.ts +++ b/packages/client/lib/cluster/multi-command.ts @@ -3,6 +3,7 @@ import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommand import RedisMultiCommand, { RedisMultiQueuedCommand } from '../multi-command'; import { extendWithCommands, extendWithModulesAndScripts } from '../commander'; import RedisCluster from '.'; +import { ExcludeMappedString } from '../client'; type RedisClusterMultiCommandSignature = (...args: Parameters) => RedisClusterMultiCommandType; @@ -12,16 +13,16 @@ type WithCommands = { }; type WithModules = { - [P in keyof M as M[P] extends never ? never : P]: { - [C in keyof M[P]]: RedisClusterMultiCommandSignature; + [P in keyof M as ExcludeMappedString

]: { + [C in keyof M[P] as ExcludeMappedString]: RedisClusterMultiCommandSignature; }; }; type WithScripts = { - [P in keyof S as S[P] extends never ? never : P]: RedisClusterMultiCommandSignature + [P in keyof S as ExcludeMappedString

]: RedisClusterMultiCommandSignature }; -export type RedisClusterMultiCommandType, S extends RedisScripts = Record> = +export type RedisClusterMultiCommandType = RedisClusterMultiCommand & WithCommands & WithModules & WithScripts; export type RedisClusterMultiExecutor = (queue: Array, firstKey?: RedisCommandArgument, chainId?: symbol) => Promise>; diff --git a/packages/client/lib/command-options.ts b/packages/client/lib/command-options.ts index 2096258046..8f91130b55 100644 --- a/packages/client/lib/command-options.ts +++ b/packages/client/lib/command-options.ts @@ -10,5 +10,5 @@ export function commandOptions(options: T): CommandOptions { } export function isCommandOptions(options: any): options is CommandOptions { - return options && options[symbol] === true; + return options?.[symbol] === true; } diff --git a/packages/client/lib/commander.ts b/packages/client/lib/commander.ts index fa1136f873..2d129d679e 100644 --- a/packages/client/lib/commander.ts +++ b/packages/client/lib/commander.ts @@ -70,7 +70,7 @@ export function extendWithModulesAndScripts(config: Exte return (Commander ?? config.BaseClass) as any; } -export function transformCommandArguments( +export function transformCommandArguments( command: RedisCommand, args: Array ): { diff --git a/packages/test-utils/lib/index.ts b/packages/test-utils/lib/index.ts index 8e20df7581..56847ce84d 100644 --- a/packages/test-utils/lib/index.ts +++ b/packages/test-utils/lib/index.ts @@ -5,12 +5,10 @@ import { RedisServerDockerConfig, spawnRedisServer, spawnRedisCluster } from './ import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; -interface TestUtilsConfig { +interface TestUtilsConfig { dockerImageName: string; dockerImageVersionArgument: string; defaultDockerVersion: string; - defaultClientOptions?: Partial>; - defaultClusterOptions?: Partial>; } interface CommonTestOptions { @@ -29,7 +27,7 @@ interface ClusterTestOptions ext numberOfNodes?: number; } -export default class TestUtils { +export default class TestUtils { static #getVersion(argumentName: string, defaultVersion: string): Array { return yargs(hideBin(process.argv)) .option(argumentName, { @@ -52,7 +50,7 @@ export default class TestUtils { readonly #DOCKER_IMAGE: RedisServerDockerConfig; - constructor(config: TestUtilsConfig) { + constructor(config: TestUtilsConfig) { this.#DOCKER_IMAGE = { image: config.dockerImageName, version: TestUtils.#getVersion(config.dockerImageVersionArgument, config.defaultDockerVersion)