简体   繁体   English

UIComponent.getRouterFor 显示 TypeScript 错误:“任何”类型值的不安全返回

[英]UIComponent.getRouterFor shows TypeScript error: Unsafe return of an 'any' typed value

I recently added TypeScript to my SAPUI5 project and having problems with the ESLint messages for the types.我最近将 TypeScript 添加到我的 SAPUI5 项目中,并且遇到类型的 ESLint 消息问题。

Considers this easy example:考虑这个简单的例子: 在此处输入图像描述

This tiny piece of code shows the error "Unsafe return of an any typed value" but I cannot figure out why.这段小代码显示错误“任何类型值的不安全返回”,但我不知道为什么。 Everything in this function has a type.此函数中的所有内容都有一个类型。 This is the description for UIComponent.getRouterFor from the package @sapui5/ts-types-esm:这是来自 @sapui5/ts-types-esm 包的 UIComponent.getRouterFor 的描述: 在此处输入图像描述

Can someone tell me what I am doing wrong?有人可以告诉我我做错了什么吗? Are the types correct?类型是否正确?

What version of the UI5 types and ESLint are you using?你使用的是什么版本的 UI5 类型和 ESLint? When I am writing the same code in my BaseController then I see another issue: @typescript-eslint/no-unnecessary-type-assertion当我在BaseController中编写相同的代码时,我看到另一个问题: @typescript-eslint/no-unnecessary-type-assertion

public getRouter(this: BaseController) {
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
    return UIComponent.getRouterFor(this) as Router;
}

which will be auto-fixed to:这将自动固定为:

public getRouter(this: BaseController) {
    return UIComponent.getRouterFor(this);
}

Due to the type inference there should be no casting necessary.由于类型推断,应该不需要强制转换。

BTW: In my case I am using ESLint 8.17.0 and OpenUI5 1.102.1.顺便说一句:就我而言,我使用的是 ESLint 8.17.0 和 OpenUI5 1.102.1。

Since the getRouterFor() method already has a declared type, the type will get inferred by typescript.由于getRouterFor()方法已经声明了类型,因此类型将由 typescript 推断。 Can you try doing something like this:你可以尝试做这样的事情:

public getRouter(this: BaseController) : Router {
 return UIComponent.getRouterFor(this) 
}

It turn out, that the linting errors came from the VSCode ESlint Extension.事实证明,linting 错误来自 VSCode ESlint Extension。

Since we are using on folder for the CAP project that contains the folders for the CDS development with typescript and also a folder for the app resources, the extension got confused.由于我们使用的是 CAP 项目的 on 文件夹,该文件夹包含用于使用 typescript 进行 CDS 开发的文件夹以及用于应用程序资源的文件夹,因此扩展变得混乱。 There are two .eslintrc.json files.有两个 .eslintrc.json 文件。 One on the root level(marked with root:true) and one for the typescript UI5 app.一个在根级别(标记为 root:true),一个用于 typescript UI5 应用程序。

I set the working directories settings of the extension like this我像这样设置扩展的工作目录设置

"eslint.workingDirectories": ["./app", "./srv", "./db"],

and now it works.现在它可以工作了。

Thank you @Peter Muessig for your input.谢谢@Peter Muessig 的意见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:ESLint:任何类型值的不安全返回 @typescript-eslint/no-unsafe-return - Typescript : ESLint : Unsafe return of an any typed value @typescript-eslint/no-unsafe-return ESLint:“any”的不安全分配和“any”类型值的不安全调用 - ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value 元素引用<any> .nativeElement:任何抛出“任何类型值的不安全调用”错误</any> - ElementRef<any>.nativeElement: any throwing "Unsafe call of an any typed value" error 收到此打字稿错误:对任何值不安全的成员访问 [key] - Getting this typescript error: Unsafe member access [key] on an any value @typescript-eslint/no-unsafe-assignment:任何值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an any value 对 Typescript function 中的 return 语句不安全地使用“any”类型的表达式 - Unsafe use of expression of type 'any' for return statements in Typescript function 任何值上的 eslint 错误不安全成员访问 ['content-type'] - eslint error Unsafe member access ['content-type'] on an any value angular 中的 TypeScript 错误:声明类型既不是“void”也不是“any”的 function 必须返回一个值 - TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value Typescript 强类型键值对声明 - Typescript strongly typed key value pair declaration Ionic 2:“警告:清理不安全的URL值”错误 - Ionic 2 : “WARNING: sanitizing unsafe URL value” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM