Skip to content

Create Redis client in TypeScript. Reis 4.0.0. #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DimitriiKr opened this issue Nov 30, 2021 · 8 comments
Closed

Create Redis client in TypeScript. Reis 4.0.0. #1748

DimitriiKr opened this issue Nov 30, 2021 · 8 comments

Comments

@DimitriiKr
Copy link

Hi all,
I am relative new to TypeScritp. That's why my question is eventually really obvious. I need help with creating a redis client in TypeScript. Especially declaring a client type:

let redisClient:  RedisClientType | undefined
  
  export async function useRedis(): RedisClientType {
      if (!redisClient){
          redisClient = createClient( { /* redis options */} )
          await redisClient.connect()
      }
  
      return redisClient
  }

With this code I receive a TypeScript error "type RedisClientType<...> is not assignable to never".

What is the right approach to instantiate redis client?
Perhaps you could just provide an TypeScript example.

Thank you in advance

@wingedrhino
Copy link

Ahoy! I'd also like inputs on this. What is the type of the object returned by createClient({url})?

@jamesarosen
Copy link

What is the type of the object returned by createClient({url})?

It's RedisClientType, which comes from @node-redis/client/dist/lib/client. The only solution I've found is

import { RedisClientType } from '@node-redis/client/dist/lib/client';

but this has two cons:

  1. I have to depend on @node-redis/client directly. It should be an implementation detail of the redis package.
  2. I'm reaching pretty far into the internals of that package.

Together, these two things mean my code is likely to break when these packages change.

@jamesarosen
Copy link

#1732 has more on this

@DimitriiKr
Copy link
Author

thank you @jamesarosen.
import { RedisClientType } from '@node-redis/client/dist/lib/client';
Is exactly the import I have. But still, if you specify type of the variable for the client, TypeScript shows a Type Error:

let redisClient : RedisClientType = createClient( {options} ) 

//=> redisClient has following error:
// Error:(12, 9) TS2322: Type 'RedisClientType<{ json: { ARRAPPEND: typeof import("C:/workspace/......   Types of property 'options' are incompatible.

So, I still don't know, how to use RedisClientType - how to specify a variable type or return type of a function.

@jamesarosen
Copy link

This worked for me:

import { createClient } from 'redis'

export type RedisClientType = ReturnType<typeof createClient>

export default async function start(): Promise<RedisClientType> {
  const redis: RedisClientType = createClient({ url:  })
  await redis.connect()
  return redis
}

@DimitriiKr
Copy link
Author

Define and export own redis client type worked for me!

export type RedisClientType = typeof client // OK

The second option to get the redis client type as ReturnType doesn't work for function return types:

export type RedisClientType = ReturnType<typeof createClient> // not working as function return type

export function useRedis() : Promise<RedisClientType>
{
...
return client   // <= Error TS2719:... Two different types with this name exist, but they are unrelated.... The types returned by 'multi()' are incompatible between these types.
}

Working example:

const client = createClient( {options} )
export type RedisClientType = typeof client 

export function useRedis() : Promise<RedisClientType>
{
...
await client.connect()
...
return client
}

@leibale
Copy link
Contributor

leibale commented Nov 30, 2021

Duplicate of #1673

@leibale leibale marked this as a duplicate of #1673 Nov 30, 2021
@leibale leibale closed this as completed Nov 30, 2021
@leviermonaites
Copy link

Guys, if you take on the approach suggested by our fellow @jamesarosen you are probably going to hit some performance issues when compiling your typescript project.

I removed this explicit workaroundy type definition and it decreased my build time from 5+ minutes to 5 seconds.

That's what I experienced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants