Ioredis: Working around Typescript and defineCommand

Created on 3 Mar 2019  路  7Comments  路  Source: luin/ioredis

Hello,

Is there a way to work around the Typescript limitations with defineCommand?

pinned

Most helpful comment

@Qix- i don't think it can be solved on ioredis side.
To overcome issues with typings you can create a wrapper file with one @ts-ignore like this:

// using "@types/ioredis": "^4.0.8"
import Redis, { Redis as RedisInterface } from 'ioredis';

// @ts-ignore
export const redis: MyRedis = new Redis();

interface MyRedis extends RedisInterface {
    // declare your custom commands here
    test();
    anotherCustomCommand();
}

and use it like this (checked in VSCode with latest typescript and they pick up test method on redis):

import { redis} from './redis-wrapper';
redis.test();

All 7 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 7 days if no further activity occurs, but feel free to re-open a closed issue if needed.

This seems to be a feature request / question that shouldn't be auto-closed without a maintainer actually looking at it.

This. Stale bot is incredibly annoying.

@Qix- what is it the problem you have with defineCommand?

@kroleg It's been a while, sorry for not putting more information into the OP - I must have been rushed or distracted when I made it.

I believe the issue was the fact that there's no way to tell Typescript that ioredis creates a new method on the client's prototype when defineCommand() is used.

Therefore, using client.defineCommand('foo', {...}) and then client.foo(...) causes Typescript to fail.

@Qix- i don't think it can be solved on ioredis side.
To overcome issues with typings you can create a wrapper file with one @ts-ignore like this:

// using "@types/ioredis": "^4.0.8"
import Redis, { Redis as RedisInterface } from 'ioredis';

// @ts-ignore
export const redis: MyRedis = new Redis();

interface MyRedis extends RedisInterface {
    // declare your custom commands here
    test();
    anotherCustomCommand();
}

and use it like this (checked in VSCode with latest typescript and they pick up test method on redis):

import { redis} from './redis-wrapper';
redis.test();

Hey thanks, didn't know you could do that!

Was this page helpful?
0 / 5 - 0 ratings