uicomponent.getRoutor show show tyspercript错误:不安全返回AN' any'键入值

发布于 2025-02-07 05:54:39 字数 531 浏览 3 评论 0原文

我最近在我的SAPUI5项目中添加了TypeScript,并在类型的ESLINT消息上遇到问题。

考虑了这个简单的例子:

这个很小的代码显示了错误“任何键入值的不安全返回”,但我不知道原因。 此功能中的所有内容都有类型。 这是uicomponent.getRoutorfor的描述,来自packem @sapui5/ts-types-esm:

有人可以告诉我我在做什么错吗?类型正确吗?

I recently added TypeScript to my SAPUI5 project and having problems with the ESLint messages for the types.

Considers this easy example:
enter image description here

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:
enter image description here

Can someone tell me what I am doing wrong? Are the types correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2025-02-14 05:54:39

您正在使用哪种版本的UI5类型和ESLINT?当我在basecontroller中编写相同的代码时,我会看到另一个问题:@typescript-eslint/no-no-unnnneconce--type-assertion

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

它将自动固定为:

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

由于推断的类型,不应有必要的铸造。

顺便说一句:就我而言,我正在使用ESLINT 8.17.0和OpenUI5 1.102.1。

What version of the UI5 types and ESLint are you using? When I am writing the same code in my BaseController then I see another issue: @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.

驱逐舰岛风号 2025-02-14 05:54:39

由于getRouterfor()方法已经具有声明的类型,因此该类型将通过Typescript推断。
您能尝试做这样的事情:

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

Since the getRouterFor() method already has a declared type, the type will get inferred by typescript.
Can you try doing something like this:

public getRouter(this: BaseController) : Router {
 return UIComponent.getRouterFor(this) 
}
提赋 2025-02-14 05:54:39

事实证明,覆盖错误来自VSCODE ESLINT扩展。

由于我们将在文件夹上用于CAP项目,该项目包含使用Typescript的CDS开发文件夹,也包含App Resources的文件夹,因此扩展名感到困惑。
有两个.eslintrc.json文件。一个在根级别(用root:true标记),一个用于Typescript UI5应用程序。

我设置了这样的扩展名的工作目录设置

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

,现在它有效。

谢谢@Peter Muessig的输入。

It turn out, that the linting errors came from the 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.
There are two .eslintrc.json files. One on the root level(marked with root:true) and one for the typescript UI5 app.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文