I recently added TypeScript to my SAPUI5 project and having problems with the ESLint messages for the types.
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:

Can someone tell me what I am doing wrong? Are the types correct?
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.
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)
}
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.