13

VS Code is showing me TS errors that suddenly became a problem but used to work fine before.

  • When I run npx tsc I get no TS errors.
  • The project compiles with no TS errors
  • When I open the file in PHPStorm, I get no TS errors

Where are these errors possibly coming from? Are they valid or is VS Code confused?

I have tried to resolve them but can't figure out how to type them (there are loads of imported types being concatenated and such).

Sometimes the errors would go away if I tried it with a different type, and then suddenly come back after a while. any seems to work though, but yeah, that's cheating :)

enter image description here

2
  • 2
    what TypeScript do you have installed? (check in node_modules/typescript/package.json or something like that path). Have you set VS Code to use your workspace's TypeScript for IntelliSense? (see the typescript.tsdk and typescript.enablePromptUseWorkspaceTsdk settings). Commented Feb 15, 2023 at 17:48
  • @user, thank you, that does resolve the issue. Do you know what version of typescript VScode is using? I guess these errors might be valid in newer versions of TS. Commented Feb 16, 2023 at 10:02

1 Answer 1

24

This can be caused by TypeScript version differences. VS Code defaults to use its bundled TypeScript installation (usually around the latest one) instead of the one specified as an NPM package dependency. You can tell VS Code to use (more accurately: to prompt you to pick) the workspace TypeScript with the following settings:

"typescript.tsdk": "./node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,

To find out the version of TypeScript that your version of VS Code bundles, use the TypeScript: Select TypeScript Version... in the command palette. The version will be shown beside the option that says "Use VS Code's Version".

Related: How do I force Visual Studio Code to always use my workspace's version of TypeScript for all projects?.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it does mean those errors are valid in a higher version of TS. Hopefully I'll be able to resolve them eventually. But that is another question.
Selecting the correct TS version is not always enough. Other answers are also given here: stackoverflow.com/q/60803223

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.