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)atthis.redisClient.getfragment
and
ESLint: Unsafe assignment of an any value.(@typescript-eslint/no-unsafe-assignment)atgetAsync
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
thisto the wrong method.In the second error, you are telling Typescript what return type
getAsyncshould hold, instead you need to tellpromisifywhat is the return value that your are trying to promisify.This should fix both eslint errors: