Closed DimitriiKr closed 2 years ago
Ahoy! I'd also like inputs on this. What is the type of the object returned by createClient({url})
?
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:
@node-redis/client
directly. It should be an implementation detail of the redis
package.Together, these two things mean my code is likely to break when these packages change.
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.
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
}
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
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
}
Duplicate of #1673
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
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:
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