Search code examples
typescriptvisual-studio-codetsc

Why do I get a TypeScript error in VS Code but not from the tsc command?


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


Solution

  • 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?.