23

Is there a way to force Visual Studio Code to use the TypeScript installed locally in a JS project (instead of the version bundled with VSCode, or the version installed globally) for type checking when editing that project?

prmph
  • 7,616
  • 11
  • 37
  • 46
  • As opposed to what? – Phix Feb 21 '19 at 15:42
  • 1
    @Phix: Please read up on the "typescript.tsdk" setting in VSCode – prmph Feb 21 '19 at 15:46
  • 2
    Possible duplicate of [What TypeScript version is Visual Studio Code using? How to update it?](https://stackoverflow.com/questions/39668731/what-typescript-version-is-visual-studio-code-using-how-to-update-it) – Heretic Monkey Feb 21 '19 at 16:10
  • 2
    This question is definitely not a duplicate of that question. The linked question is asking about how to find which version of TypeScript is being used. My question is about how to force a certain version, namely the version that has been installed in the _node_modules_ folder of the project being edited. The answers about selecting another version do not cover this scenario I am interested in. – prmph Feb 21 '19 at 16:24
  • 1
    Related to https://stackoverflow.com/questions/49949148/how-to-use-typescript-version-defined-in-settings-json-by-default-in-vscode and https://stackoverflow.com/questions/74642723/how-do-i-force-vscode-to-always-use-my-workspaces-version-of-typescript-for-all – somethingRandom Apr 10 '23 at 06:31

2 Answers2

24

I assume that you have installed TypeScript in the myProject directory.

cd myProject
npm install --save-dev typescript

If you have already done that, then add a .vscode/ directory with a settings.json file that specifies the TypeScript SDK to use:

{
    "typescript.tsdk": "node_modules\\typescript\\lib",
    "typescript.enablePromptUseWorkspaceTsdk": true,
}

This is the final directory structure:

myProject
  .vscode
    settings.json
  node_modules
    typescript
      lib

Important: make sure that you open VSCode in the myProject directory!

The VS Code documentation calls this "using the workspace version of TypeScript."

Knaģis
  • 20,827
  • 7
  • 66
  • 80
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • @Luttin: I tried this, then re-started VSCode, but I still don't get the option to use my project's version. FYI, In my dev dependencies in my package.json, I have installed version 3.2.2. But the version VSCode is using is 3.3.1. – prmph Feb 21 '19 at 17:12
  • What folder are you opening in VSCode? Does that folder contain the `.vscode/` directory? For instance, if you have `myProject/.vscode/settings.json`, are you opening the `myProject` directory in VSCode? – Shaun Luttin Feb 21 '19 at 17:23
  • @Luttin: Yes, that's exactly my setup – prmph Feb 21 '19 at 21:54
  • You have an interesting mystery on your hands. If you have the time, please share a repository with your setup so that I can try it on my local machine. Also, what version of VSCode are you using? – Shaun Luttin Feb 21 '19 at 22:13
  • @Luttin, sorry it works now. Turns out I opened a super-folder of the project folder. – prmph Feb 21 '19 at 23:11
  • @Luttin The syntax for settings.json in the documentation (and what worked for me) is `{"typescript.tsdk": "./node_modules/typescript/lib"}` – Andreas Larsen Nov 19 '19 at 16:34
  • 1
    Noting here for future searches that you *must* open the folder which contains the .vscode/ folder or the settings.json file, not a saved workspace from a .code-workspace file. It will completely ignore typescript.tsdk in .code-workspace files and ignore settings.json files when opening from a saved workspace, even if the workspace only contains that folder as its only root. You must open the folder for it to read the setting and show the workspace version in the Select Typescript Version options. – Qwertronix Jun 07 '20 at 03:40
  • Unfortunately the documentation says that this setting does not force the use of the specified TS installation: *"The typescript.tsdk workspace setting only tells VS Code that a workspace version of TypeScript exists. To actually start using the workspace version for IntelliSense, you must run the TypeScript: Select TypeScript Version command and select the workspace version."* – Knaģis Apr 13 '23 at 07:58
  • So what if the project is in a subfolder not the root folder and I want this project to use the workspace version? – dragonfly02 May 30 '23 at 05:20
1

Shaun Luttins answer worked for me except for one detail: Using Version 1.31.1 of Visual Studio Code file settings.json only works if placed directly in the root folder of the workspace, not in folder .vscode.

Martin Pabst
  • 861
  • 11
  • 9