redis 客户端中的 Promisify 违反了 eslint 规则
Promisify in redis client violates eslint rules
我想使用 redis 客户端 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);
// ...
};
}
但是我 运行 遇到了两个关于 eslint 的问题:
ESLint: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.(@typescript-eslint/unbound-method)
在 this.redisClient.get
片段
和
ESLint: Unsafe assignment of an any value.(@typescript-eslint/no-unsafe-assignment)
在 getAsync
我如何使用 promisify
来识别类型和适当的 this
范围?
在您的第一个 eslint 错误中,您将 this
绑定到错误的方法。
在第二个错误中,您告诉 Typescript return 类型 getAsync
应该包含什么,而您需要告诉 promisify
您的 return 值是什么正在努力承诺。
这应该可以修复两个 eslint 错误:
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))
};
}
我想使用 redis 客户端 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);
// ...
};
}
但是我 运行 遇到了两个关于 eslint 的问题:
ESLint: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.(@typescript-eslint/unbound-method)
在this.redisClient.get
片段
和
ESLint: Unsafe assignment of an any value.(@typescript-eslint/no-unsafe-assignment)
在getAsync
我如何使用 promisify
来识别类型和适当的 this
范围?
在您的第一个 eslint 错误中,您将 this
绑定到错误的方法。
在第二个错误中,您告诉 Typescript return 类型 getAsync
应该包含什么,而您需要告诉 promisify
您的 return 值是什么正在努力承诺。
这应该可以修复两个 eslint 错误:
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))
};
}