如何修复 no-unsafe-any 规则?
How to fix no-unsafe-any rule?
我正在使用 TSLint to lint my Angular TypeScript code. I enabled no-unsafe-any
规则,因为对我来说这似乎是一个很好的规则,永远不要对 any
.
类型的属性做任何假设
问题是规则报告了我的某些代码的错误,除了禁用规则之外我无法以任何方式修复。根据以下规则无效的代码示例。
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 个错误:
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 库函数的处理程序的 error
参数具有 any
类型。我知道 error
可以是任何类型,所以假设它定义了任何属性是不安全的,但我在实际引用它们之前首先检查这些属性是否存在,这对我来说似乎是安全的。
我如何 can/should 说服 linter/TypeScript 编译器它是安全的并通过规则?
你有两个选择,当你知道 error
总是有一个特定的类型时,你可以只注释类型。如果不能确定,可以用type guard.
类型注释
使用类型注释,您可以简单地告诉编译器您期望error
是某种类型。您可以使用这种方法完全避免 any
类型:
interface Error {
status: string,
statusText: string,
error: { message: string | undefined } | undefined;
}
catchError((error: Error | undefined) => {
//...
}
类型保护
只要值可能是某种类型,但不一定有是,你就可以使用类型保护那种。类型保护将检查类型,并在以下块中,变量将是该检查的类型:
function isError(value: any | undefined): value is Error {
return error && ((error as Error).status !== undefined);
}
catchError(error => {
if (isError(error)) {
//Inside this block, error will be of type Error
}
}
在 Angular 的情况下,错误应始终属于 HttpErrorResponse
类型
catchError((error: HttpErrorResponse) => {
//...
}
就是说,在您的代码中,您查看了 error.error
,它在 HttpErrorResponse
中被定义为 any
,因此您可能应该使用类型保护来检查并将其转换为 Error 对象.不是没有必要定义 Error
- 它应该由打字稿基本类型定义。
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;
我正在使用 TSLint to lint my Angular TypeScript code. I enabled no-unsafe-any
规则,因为对我来说这似乎是一个很好的规则,永远不要对 any
.
问题是规则报告了我的某些代码的错误,除了禁用规则之外我无法以任何方式修复。根据以下规则无效的代码示例。
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 个错误:
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 库函数的处理程序的 error
参数具有 any
类型。我知道 error
可以是任何类型,所以假设它定义了任何属性是不安全的,但我在实际引用它们之前首先检查这些属性是否存在,这对我来说似乎是安全的。
我如何 can/should 说服 linter/TypeScript 编译器它是安全的并通过规则?
你有两个选择,当你知道 error
总是有一个特定的类型时,你可以只注释类型。如果不能确定,可以用type guard.
类型注释
使用类型注释,您可以简单地告诉编译器您期望error
是某种类型。您可以使用这种方法完全避免 any
类型:
interface Error {
status: string,
statusText: string,
error: { message: string | undefined } | undefined;
}
catchError((error: Error | undefined) => {
//...
}
类型保护
只要值可能是某种类型,但不一定有是,你就可以使用类型保护那种。类型保护将检查类型,并在以下块中,变量将是该检查的类型:
function isError(value: any | undefined): value is Error {
return error && ((error as Error).status !== undefined);
}
catchError(error => {
if (isError(error)) {
//Inside this block, error will be of type Error
}
}
在 Angular 的情况下,错误应始终属于 HttpErrorResponse
类型catchError((error: HttpErrorResponse) => {
//...
}
就是说,在您的代码中,您查看了 error.error
,它在 HttpErrorResponse
中被定义为 any
,因此您可能应该使用类型保护来检查并将其转换为 Error 对象.不是没有必要定义 Error
- 它应该由打字稿基本类型定义。
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;