Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Every time I open a new project in Visual Studio Code, it defaults to its own version of TypeScript rather than the workspace's version. Is there a way to default to the workspace version, so I don't have to change it every time?

For example,

Changing to workspace version

I've tried looking through the settings and there only seems to be an ability to prompt me—none to default to the behaviour I want every time.

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

like image 527
ShaneQful Avatar asked Nov 20 '25 21:11

ShaneQful


1 Answers

I think the best you can currently do at the time of this writing (when the latest VS Code version is 1.83) is (in settings.json):

"typescript.tsdk": "./node_modules/typescript/lib",
// ^ pretty sure this needs to go in workspace settings

"typescript.enablePromptUseWorkspaceTsdk": true,
// ^ this can go in user settings, but you may as well put it in each
//   workspace's settings so collaborators don't need to know about it.

The settings' descriptions, respectively:

Specifies the folder path to the tsserver and lib*.d.ts files under a TypeScript install to use for IntelliSense, for example: ./node_modules/typescript/lib.

  • When specified as a user setting, the TypeScript version from typescript.tsdk automatically replaces the built-in TypeScript version.
  • When specified as a workspace setting, typescript.tsdk allows you to switch to use that workspace version of TypeScript for IntelliSense with the TypeScript: Select TypeScript version command.

See the TypeScript documentation for more detail about managing TypeScript versions.


Enables prompting of users to use the TypeScript version configured in the workspace for Intellisense.

You can actually use that setting in your user settings.json file- not just the workspace .vscode/settings.json file. It seems that wherever you put it, a relative path will be treated as relative to the current workspace folder.

There is currently no setting to get the workspace to use the workspace's TypeScript SDK without a prompt.

From my quick test, it doesn't work to use a path relative to the current workspace in user settings (as opposed to workspace settings) for typescript.tsdk (unfortunate, since user settings is automatic according to docs, which I assume means it doesn't require a prompt).

As for the current reason why, before the typescript.enablePromptUseWorkspaceTsdk setting existed (though I assume the logic of the comment still applies today), a maintainer has once commented:

We support setting typescript.tsdk in the workspace settings but for security reasons we always require that users opt into using the workspace version of typescript. That's where local storage comes into play.

and later elaborated:

The prompt is basically asking: do you trust this workspace and where it came from. The workspace version of TypeScript can also execute other files from the workspace, so we cannot reasonably ensure that VS Code only runs trusted files. We default to doing the safe thing: don't load anything from the workspace. No matter what, switching to load any code from the workspace must be opt-in and it must be based on a user action.

The most reasonable solution here is add a workspace setting that shows the select tsdk prompt automatically when a user first opens a workspace

That's how the typescript.enablePromptUseWorkspaceTsdk came to be.


There's a feature-request you might be interested in: VSCode should default to using the TypeScript version in the current project #172732

Quoting from one of the maintainers' reponses to that feature-request:

We've discussed this previously but decided against this for a few reasons:

  • Workspace versions of TS tend to get out of date. Old TS versions have bugs and performance issues that have often been fixed by our bundled version. We don't make any fixes to old TS versions and issues reports from these old versions are also generally not helpful

  • While there are cases where using a different TS version for editing vs compiling matters, it usually doesn't.

    On your note:

    VSCode was very slow to update the version of TypeScript

    The stable version of VS Code typically picks up the latest stable Typescript within one to two weeks. The latest TS is typically in VS Code insiders after one day. It sounds like in your case you were trying to use features from an upcoming TS beta or RC

    I don't think it's a big obstacle for advanced users who need the latest TS right away to switch to use the workspace version of TS

The compromise is the setting you already called out: typescript.enablePromptUseWorkspaceTsdk. This makes it easier for advanced users to switch, without effecting the experience of the vast majority of users who do not need to switch to the workspace version (and for whom switching could actually result in a worse user experience)

Here's another feature-request you'll probably be interested in: Combine workspace trust with enablePromptUseWorkspaceTsdk #135713. Quoting from that feature-request:

Now that we have workspace trust feature in VSCode. The separate prompt of enablePromptUseWorkspaceTsdk felt somewhat redundant. Can we automatically honor typescript.tsdk in workspace setting when user already trust the workspace?

This doesn't regress on security and makes distributing typescript.tsdk among our large developer user base much easier.

Where there is talk of going through and implementing it (with some blocking design questions) (see this), and a draft Pull Request: fix(typescript-language-features): tsdk trust #190057.

like image 137
starball Avatar answered Nov 23 '25 21:11

starball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!