I wanted to use redis client with promisify
:
import type { RedisClient } from "redis";
import * as util from "util";
class MyClass {
private redisClient: RedisClient;
constructor(client: RedisClient) {
this.redisClient = client;
}
getMyData = (): SomeType[] => {
const getAsync: Promise<string | undefined> = util.promisify(this.redisClient.get).bind(this.redisClient);
// ...
};
}
However I ran into two issues with eslint:
ESLint: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.(@typescript-eslint/unbound-method)
at this.redisClient.get
fragmentand
ESLint: Unsafe assignment of an any value.(@typescript-eslint/no-unsafe-assignment)
at getAsync
How I can use promisify
in a way that will be type aware and proper this
scoping?
In your first eslint error, you are binding this
to the wrong method.
In the second error, you are telling Typescript what return type getAsync
should hold, instead you need to tell promisify
what is the return value that your are trying to promisify.
This should fix both eslint errors:
import type { RedisClient } from "redis";
import * as util from "util";
class MyClass {
private redisClient: RedisClient;
constructor(client: RedisClient) {
this.redisClient = client;
}
getMyData = (): SomeType[] => {
const getAsync = util.promisify<string |undefined>(this.redisClient.get.bind(this.redisClient))
};
}