Skip to content

Incorrect ts7022 error emitted by tsserver (not by tsc) #57429

Closed
@martaver

Description

@martaver

🔎 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):

  1. Clone https://github.com/cleric-sh/repro/tree/phantom-ts7022-error-in-vscode
  2. Ensure switched to branch phantom-ts7022-error-in-vscode
  3. npm i
  4. npm run compile --> observe no compiler errors
  5. Open index.ts (in VSCode or another editor utilizing tsserver)
  6. Error may or may not appear on line 26 on value (depending on whether analysis is on first pass or not)
  7. Make a change to the file (e.g. add a space) to trigger tsserver to re-analyse the file.
  8. Error should definitely be visible now.
  9. 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:

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

https://tsplay.dev/WkqM2N

💻 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 to any, since ThisType isn't used, and thus value should also be any.
  • this resolve to IThing.

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 be any since this 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions