ioredis-mock icon indicating copy to clipboard operation
ioredis-mock copied to clipboard

Are typings available?

Open quinnlangille opened this issue 6 years ago • 20 comments

I'm trying to use this package in a typescript application but can't find type declarations anywhere. Just making sure they don't exist before I create some.

quinnlangille avatar Jan 14 '19 21:01 quinnlangille

Last I checked there wasn't any, but I noticed that there's a @types/ioredis already.

Even though my long term goal for this library is to move all of it to TypeScript I would love to see a PR with definitions. That way TS users can benefit from it in the meantime. And when work starts on moving the source to TS the library definitions will be a great starting point 😄

stipsan avatar Jan 15 '19 09:01 stipsan

Yeah I can make the PR no problem, will work on it this week. I'll make it to definitelyTyped for now, just for ease of install. Once the library moves to TS we can migrate here

quinnlangille avatar Jan 15 '19 15:01 quinnlangille

Ok cool 👍

stipsan avatar Jan 16 '19 08:01 stipsan

Hi @quinnlangille, @stipsan Any ETA on this?

skyzenr avatar Mar 08 '19 16:03 skyzenr

Any update on this?

datdevboi avatar Mar 20 '19 16:03 datdevboi

Negative, I haven't had time I expected to work on it - however there is an increasing need in office for a solution, so I may get it done soon. If anyone is needed typings now and want them done fast, feel free to take on the task! Just ping here to keep everyone informed

quinnlangille avatar Mar 20 '19 16:03 quinnlangille

Here's a workaround that I'm using in the mean time. It assumes you have ioredis and @types/ioredis installed in addition to iosredis-mock.

import * as IORedis from 'ioredis';

const MockIORedis = require('ioredis');
const mockRedisClient = new MockIORedis() as IORedis.Redis;

steveklebanoff avatar Apr 17 '19 18:04 steveklebanoff

@stipsan I just saw your comment, I'm working on a PR with definitions but I'd like to ensure some assumptions I've made:

  1. ioredis-mock have the commands listed here and are equivalent with its ioredis commands.
  2. Since multi() and exec() are supported, pipelining is supported and work exactly equal than ioredis pipelining with the commands that ioredis-mock supports.

Once I finish the definitions, I have two options:

  1. Submit a PR to the DefinitelyTyped repo. I'd love to show you the definition file before doing that.
  2. Submit a PR to this repo and put the definitions under a "types" folder. This approach is not my favourite one, its used in the knex repo but I do prefer to have type definitions in the DefinitelyTyped repo because is a the standard way, its transparent to JS users and does not require to change the typeRoots option of the TS project.

¿Are my assumptions right? ¿How should I provide you the definitions?

notaphplover avatar Aug 24 '19 08:08 notaphplover

@stipsan I submitted the PR #849 in order to show you the type definitions. If everything is OK we can merge the definitions or submit a PR to the DefinitelyTyped repo.

notaphplover avatar Aug 26 '19 14:08 notaphplover

@notaphplover any news on the PR in the DefinitelyTyped repo?

CarsonMcKinstry avatar Feb 04 '20 09:02 CarsonMcKinstry

@CarsonMcKinstry wow I really forgot that, sorry. I've been a bit busy but I think I'll have enough time this weekend.

notaphplover avatar Feb 05 '20 08:02 notaphplover

@notaphplover Let us know if you need a hand, I'm also interested in this :smile:

robcresswell avatar Feb 10 '20 19:02 robcresswell

@notaphplover any updates on this? I couldn't find the PR on the DefinitelyTyped repository. :'(

@steveklebanoff aren't you basically just creating an instance of IORedis rather than IORedis-Mock? I'm not sure what I'm missing there?

import RedisMock from 'ioredis-mock';
import { Redis as IORedis } from 'ioredis';
const Redis = RedisMock as IORedis;

Doesn't work

Conversion of type '(...args: any[]) => any' to type 'Redis' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

Type '(...args: any[]) => any' is missing the following properties from type 'Redis': Promise, status, connect, disconnect, and 200 more.ts(2352)

import RedisMock from 'ioredis-mock';
import * as IORedis from 'ioredis';
const Redis = RedisMock as IORedis;

Doesn't work because

Cannot use namespace 'IORedis' as a type.

Same goes for

import RedisMock from 'ioredis-mock';
import * as IORedis from 'ioredis';
import * as IORedisMock from 'ioredis-mock';

const TempIORedis = IORedisMock as unknown;
const Redis = TempIORedis as IORedis;

chanced avatar Jul 11 '20 18:07 chanced

@chanced

This seems to work for me as a workaround:

import * as IORedis from 'ioredis'

const IoRedisMock: typeof IORedis = require('ioredis-mock')

havsar avatar Nov 25 '20 20:11 havsar

@arctouch-gabrielluchtenberg @havsar You can use the IORedis types, but the mock doesn't fully cover it / has some differences, which are documented, so your types will be wrong and lead to some hard to identify issues.

robcresswell avatar Nov 26 '20 07:11 robcresswell

@notaphplover Do you have any news for this? Cheers.

Theo-Evrard avatar Jan 29 '21 04:01 Theo-Evrard

@Theo-Evrard I'm so sorry, the definition file is in this PR, feel free to do the PR to DefinitelyTyped. It's been a while and some definitions probably needs an update, but I would hazard to say it's better than having no types. I've been wondering if maybe types should be included in this repo:

  1. npm @types/foo versions probably won't match npm foo versions.
  2. It's easier to submit an issue in this way. If you have an issue with the @types/foo library you need to know who are the contributors to the library and mention them.

I know it's the opposite I said previously, but now I think it's the best way.

I'm sorry for half and a year of silence, I have no excuses at all (even if it's true I've been busy)

notaphplover avatar Jan 29 '21 09:01 notaphplover

Hey, so there's the data property available on the redis-mock options but it isn't available on ioredis types 🙂

crispinkoech avatar Sep 30 '21 10:09 crispinkoech

updates: it's available on npm now, you can just use @types/ioredis-mock


I create a PR at DefinitelyTyped but it's not merged yet. https://github.com/DefinitelyTyped/DefinitelyTyped/pull/63499

You can add this snippet to your project

ioredis-mock.d.ts

declare module 'ioredis-mock' {
  import ioredis = require('ioredis');

  type RedisOptions = { data?: Record<string, unknown> } & ioredis.RedisOptions;

  interface Constructor {
    new (port: number, host: string, options: RedisOptions): ioredis.Redis;
    new (path: string, options: RedisOptions): ioredis.Redis;
    new (port: number, options: RedisOptions): ioredis.Redis;
    new (port: number, host: string): ioredis.Redis;
    new (options: RedisOptions): ioredis.Redis;
    new (port: number): ioredis.Redis;
    new (path: string): ioredis.Redis;
    new (): ioredis.Redis;
  }

  const redisMock: Constructor;
  export default redisMock;
}

trim21 avatar Dec 09 '22 22:12 trim21