mmkal / handy-redis

A wrapper around node_redis with Promise and TypeScript support.
Apache License 2.0
172 stars 10 forks source link

Having trouble using the constructor w/ ClientOps #14

Closed sjmcdowall closed 6 years ago

sjmcdowall commented 6 years ago

I am sure just is this a user error -- but I have some pretty simple TS code and it's throwing me a TS error. The code and error follow:

import { createHandyClient, IHandyRedis } from 'handy-redis';
import { BaseStore } from 'koa-session-ts';
import { ClientOpts } from '../../node_modules/@types/redis';

/**
 * RedisStore for koa-session
 *
 * @param {object} options the options pass to node_redis
 * @returns RedisStore instance
 */
export class RedisSessionStore extends BaseStore {
  // Class members
  readonly client: IHandyRedis;

  constructor(options?: ClientOpts) {
    super();

    this.client = createHandyClient(options);   // Error is here at options

It doesn't like the options I pass in ..following problem

I also tried to set the options to type ICreateHandyClient but effectively the same error..

I am using TS 2.9.2

[ts]
Argument of type 'ClientOpts | undefined' is not assignable to parameter of type 'RedisClient'.
  Type 'undefined' is not assignable to type 'RedisClient'.
(parameter) options: ClientOpts | undefined

It looks like it's taking only the last definition in the Interface or something .. ??

Again, I am probably just doing something wrong here ...

Thanks

mmkal commented 6 years ago

Hi - your code is working for me with this change:

import { createHandyClient, IHandyRedis } from 'handy-redis';
import { BaseStore } from 'koa-session-ts';
-import { ClientOpts } from '../../node_modules/@types/redis';
+import { ClientOpts } from 'redis';

The @types/redis package is actually a prod dependency for handy-redis (this might change in a future major version of this library though). If this still doesn't work for you, try npm uninstall @types/redis, and feel free to reopen this ticket if you have problems after that.

sjmcdowall commented 6 years ago

Thanks! Sort of a newbie bonehead move!!