typescript - Redis 客户端中的 Promisify 违反了 eslint 规则

标签 typescript eslint node-redis typescript-eslint node-promisify

我想将 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 的两个问题:

  1. ESLint:避免引用未绑定(bind)的方法,这可能会导致无意中在 this.redisClient.get 片段中确定“this”的范围。(@typescript-eslint/unbound-method)

  • ESLint:任意值的不安全赋值。(@typescript-eslint/no-unsafe-assignment) at getAsync
  • 如何以类型感知和正确的 this 范围的方式使用 promisify

    最佳答案

    在第一个 eslint 错误中,您将 this 绑定(bind)到了错误的方法。

    在第二个错误中,您告诉 Typescript getAsync 应该保存什么返回类型,而您需要告诉 promisify 您尝试 promise 的返回值是什么.

    这应该可以修复两个 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))
    
      };
    }
    

    关于typescript - Redis 客户端中的 Promisify 违反了 eslint 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66472406/

    相关文章:

    javascript - 箭头函数解析错误: Unexpected token >

    node.js - 如何在通过 soket.io 获取客户端后从 redis 中删除数据?

    html - 如何检测用户何时停止滚动 - Angular 5

    typescript - 如何处理 TypeScript 中 document.getElementById() 方法类型转换期间的 ESLint 错误?

    typescript - Vue js 3 - 属性 'projects' 在类型“CreateComponentPublicInstance<{}、{}、{}、{}、{}”上不存在,

    node.js - 如何设置一个新的key Redis回调

    redis - 使用 redis pub-sub 进行 key 过期

    javascript - 遇到恼人的问题 : momentjs taking a day off by 1 with mat-datepicker for different timezones?

    typescript - 如何在zkSync中等待tx确认?

    javascript - String.Replace 仅替换第一次出现的匹配字符串。如何替换*所有*事件?