I am having a strange issue on TypeScript. I recently learnt about void ...
operator because I need to apply it so eslint
wouldn't report no-floating-promises
. However this particular snippet somehow caused an issue that I cannot reproduce on TypeScript playground:
class A {
async a() {}
async onTickAsync(repeat: boolean) {
try {
await this.a();
} catch(e) {
console.error(e);
} finally {
if (repeat) {
window.setTimeout(() => void this.onTickAsync(true), 200);
}
}
}
}
VS Code would report this error:
TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
However, the issue is not reproducible on TS Playground. Both VS Code and Playground is using TypeScript 4.5.4. Here's my tsconfig.json
:
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"sourceMap": true,
"target": "ESNext",
"module": "ESNext"
},
"exclude": [
"node_modules"
]
}
I understand it can be fixed by adding : void
return type or removing void
operation or removing noImplicitAny
:
window.setTimeout((): void => void this.onTickAsync(true), 200);
I want to ask: What causes the error? Why is it happening only on my IDE/local and not the playground?
To be sure it's not just due to VS Code, I ran tsc --version
and tsc
on a separate Terminal as well:
tsc --showConfig
output:
PS C:\Users\lukev\Downloads\Temp> tsc --showConfig
{
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"sourceMap": true,
"target": "esnext",
"module": "esnext"
},
"files": [
"./test.ts"
],
"exclude": [
"node_modules"
],
"compileOnSave": true
}
It's also interesting that it doesn't happen to other function. This, for example, does not produce any error. It seems to be something with window.setTimeout
. I found out there is something different between Function
type and () => void
for example):
class A {
doSomething1(_: Function) { }
doSomething2(_: () => any) { }
doSomething3(_: () => void) { }
async a() { }
async onTickAsync(repeat: boolean) {
// Only this one produces error
this.doSomething1(() => void this.onTickAsync(true));
this.doSomething2(() => void this.onTickAsync(true));
this.doSomething3(() => void this.onTickAsync(true));
}
}
tsc --showConfig
in your question? – Granada4.5.4 — TypeScript version
. Also I think it's not due to VS Code since it happens to independent terminal as well. – Spiremetsc
separately; I get it now.) Thanks! – NainsooksetTimeout
usingTimerHandler = string | Function
as its parameter type. Could be something difference between Playground and mylib.dom.d.ts
? – Spireme