Description
🔎 Search Terms
incorrect ts7022, wrong ts7022, tsserver only error ts7022
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'Type System Behavior'.
Summary: tsserver
emits incorrect TS7022 error, meanwhile tsc
and TS Playground
behave as expected.
Steps to Reproduce (NOTE: the error is NOT reproducible in TS Playground):
- Clone https://github.com/cleric-sh/repro/tree/phantom-ts7022-error-in-vscode
- Ensure switched to branch
phantom-ts7022-error-in-vscode
npm i
npm run compile
--> observe no compiler errors- Open
index.ts
(in VSCode or another editor utilizingtsserver
) - Error may or may not appear on line 26 on
value
(depending on whether analysis is on first pass or not) - Make a change to the file (e.g. add a space) to trigger tsserver to re-analyse the file.
- Error should definitely be visible now.
- Restart the TS Language Server and error disappears. Make a change, and error re-appears.
The code in this repro emits an error that is only visible in VSCode whose intellisense is server by tsserver
.
To compare, the same error is not emitted by tsc
for the same source and configuration.
I have attempted to raise this issue at:
- Incorrect ts7022 error shown in vscode (not by tsc) vscode#195075, and
- Incorrect TS7022 error displayed by Language Server in VSCode typescript-language-server/typescript-language-server#766
In typescript-language-server
I was informed that the error is actually produced in tsserver
and so I should raise this with the typescript
team, so here I am!
⏯ Playground Link
💻 Code
function Builder<I>(def: I) {
return def;
}
interface IThing {
doThing: (args: { value: object }) => string
doAnotherThing: () => void
}
Builder<IThing>({
doThing(args: { value: object }) {
/**
* ts7022 is shown on 'value' below.
*
* Only shown when func param 'arg' type is declared. Removing the declaration resolves the error.
* Only shown in vscode. tsc compiles without errors.
* Only shown on second pass, after a change has been made to the file. Initial type check on load shows no errors.
* Only shown when noImplicitAny: true
*
* In a more complex file, declaring this function
* after 'doAnotherThing' prevents the error being
* displayed.
*/
const { value } = this.args
return `${value}`
},
doAnotherThing() { },
})
🙁 Actual behavior
Error is shown on Line 26:
'value' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)
🙂 Expected behavior
Behaviour in tsserver
to match tsc
(which does not emit the error).
Not sure exactly what should happen.... I would imagine one of:
this
resolves toany
, sinceThisType
isn't used, and thusvalue
should also beany
.this
resolve toIThing
.
But the ts7022
error shouldn't be displayed.
Additional information about the issue
The error appears with all extensions disabled.
The error appears with all versions of typescript
(I have checked all versions since 5.2.2
), including 5.4.0-beta
.
The error does NOT appear in TS Playground: https://tsplay.dev/WkqM2N
I've noted that the error is:
- Only shown when func param 'arg' type is declared. Removing the declaration resolves the error.
- Only shown in vscode. tsc compiles without errors.
- Only shown on second pass, after a change has been made to the file. Initial type check on load shows no errors.
- Only shown when noImplicitAny: true
Also, in a more complex file, declaring this function after doAnotherThing prevents the error being displayed.
There is generally a lot of strange behaviour in this scenario of using 'this' in a non-class context. The repro is non-sensical, but is the simplest reproduction of the same error I could find.
At least this repro demonstrates:
- the error doesn't seem to make sense. e.g. why wouldn't
value
just beany
sincethis
isn't typed? - the error is inconsistent:
- disappearing if (for example) the returned object from
Builder
is assigned to a variable. - not emitted by TSC or visible in TS Playground
- disappearing if (for example) the returned object from