Skip to content

RedisClientType hard to use with createClient #1865

Closed
@tobiasdiez

Description

@tobiasdiez

The RedisClientType has two generic parameters that make it hard to use to signify the return type of createClient. In particular, the first argument modules in the declaration of createClient is a local variable that is not exported and thus cannot be easily reused in one's own code.

Maybe it's already enough to provide sufficiently general defaults for the generic arguments. If I'm not mistaken these parameters control more the configuration of the client and don't really influence the typing of the "normal" methods; so they are not that important for an user of a factory method.

Below is a similar observation from #1673.


The RedisClientType exported since 4.0.1 still does not work for me. Could not figure out how to make the exported RedisClientType, RedisClientOptions and the type returned by createClient compatible. Here is an example that illustrates the issue.

The following does not compile unless someone knows how to type RedisClientOptions correctly (refer to the code sandbox link above).

import { createClient, RedisClientOptions, RedisClientType } from "redis";

const factory = (options: RedisClientOptions<any, any>): RedisClientType => {
  return createClient(options);
};

const client: RedisClientType = factory({...});

Instead, I am doing this for now:

import { createClient} from "redis";
type RedisClientType = ReturnType<typeof createClient>;
type RedisClientOptions = Parameters<typeof createClient>[0];

const factory = (options: RedisClientOptions): RedisClientType => {
  return createClient(options);
};

const client: RedisClientType = factory({...});

Originally posted by @tbinna in #1673 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions