如何修复 no-unsafe-any 规则? [英] How to fix no-unsafe-any rule?

查看:51
本文介绍了如何修复 no-unsafe-any 规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TSLint 来整理我的 Angular TypeScript 代码.我启用了 no-unsafe-any 规则,因为在我看来,永远不要对 any 类型的属性做任何假设是一个很好的规则.

I'm using TSLint to lint my Angular TypeScript code. I enabled no-unsafe-any rule, as it seems like a good rule to me to never assume anything about properties of type any.

问题是规则报告了我的某些代码的错误,除了禁用规则之外,我无法以任何方式修复这些错误.根据以下规则无效的代码示例.

The problem is the rule reports errors on some of my code, which I'm unable to fix in any way other than disabling the rule. Example of a code that's invalid according to that rule below.

public intercept(request: HttpRequest<{}>, next: HttpHandler): Observable<HttpEvent<{}>> {
  return next
    .handle(request)
    .pipe(
      catchError(error => {
        if (error && error.status === httpCodeUnauthorized) {
          // Auto logout if unathorized
          this.authenticationService.logout();
        }

        const errorMessage = (error.error && error.error.message) || error.statusText;

        return throwError(errorMessage);
      }),
    );
}

Linter 在 2 行上报告了 4 个错误:

Linter reports 4 errors on 2 lines:

ERROR: /home/robert/programming/npc/gui/src/app/core/authentication/unauthorized.interceptor.ts[24, 24]: Unsafe use of expression of type 'any'.
ERROR: /home/robert/programming/npc/gui/src/app/core/authentication/unauthorized.interceptor.ts[29, 33]: Unsafe use of expression of type 'any'.
ERROR: /home/robert/programming/npc/gui/src/app/core/authentication/unauthorized.interceptor.ts[29, 48]: Unsafe use of expression of type 'any'.
ERROR: /home/robert/programming/npc/gui/src/app/core/authentication/unauthorized.interceptor.ts[29, 72]: Unsafe use of expression of type 'any'

2 有问题的行是:

  • if (error && error.status === httpCodeUnauthorized) {
  • const errorMessage = (error.error && error.error.message) ||error.statusText;

问题的根源在于catchError(Rxjs 库函数)有 any 类型.我知道 error 可以是任何类型,因此假设它定义了任何属性是不安全的,但我在实际引用它们之前首先检查这些属性的存在,这对我来说似乎是安全的.

The root of the problem is that error argument of a handler given to catchError (Rxjs library function) has any type. I understand error can be of any type, so it's unsafe to assume it has any properties defined, but I'm first checking for the presence of those properties before actually refering to them, which seems safe to me.

我可以/应该怎么做才能让 linter/TypeScript 编译器相信它是安全的并通过规则?

What can/should I do to convince the linter/TypeScript compiler that it's safe and pass the rule?

推荐答案

在 Angular 的情况下,错误应该总是属于 HttpErrorResponse

In case of Angular the error should always be in of type HttpErrorResponse

catchError((error: HttpErrorResponse) => {
//...
}

也就是说,在您的代码中,您会查看 error.error,它在 HttpErrorResponse 中定义为 any 因此您可能应该使用 type守卫检查并将其转换为 Error 对象.不是没有必要定义 Error - 它应该由 typescript 基本类型定义.

That said, in your code you look into error.error which is defined as any in HttpErrorResponse thus there you should probably use type guard to check and cast it to Error object. Not there is no need to define the Error - it should be defined by typescript base types.

function isError(value: any | undefined): value is Error {
  return error && ((error as Error).message !== undefined);
}

然后在

const errorMessage = isError(error.error) ? error.error.message : error.statusText;

这篇关于如何修复 no-unsafe-any 规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆