简体   繁体   English

Visual Studio Code 使用的是什么 TypeScript 版本? 如何更新它?

[英]What TypeScript version is Visual Studio Code using? How to update it?

How can I tell what version of TypeScript is being used in Visual Studio Code?如何判断 Visual Studio Code 中使用的是哪个版本的 TypeScript? In particular, I had been using TypeScript 1.8.10 and VSCode 1.4.0.特别是,我一直在使用 TypeScript 1.8.10 和 VSCode 1.4.0。 I first updated VSCode to the latest version, which was 1.5.3.我首先将 VSCode 更新到了最新版本,即 1.5.3。 But checking from the command line, I saw that my TypeScript version was still 1.8.10.但是从命令行查看,我看到我的TypeScript版本还是1.8.10。 So I updated TypeScript from the command line, and it is now 2.0.3.所以我从命令行更新了 TypeScript,现在是 2.0.3。

Is there a way to tell for sure whether Visual Studio Code is using version 2.0.3?有没有办法确定 Visual Studio Code 是否使用 2.0.3 版?

Is there a method for updating Visual Studio Code that will automatically update TypeScript to the latest released version, or does the TypeScript update have to be done independently?是否有更新 Visual Studio Code 的方法会自动将 TypeScript 更新到最新发布的版本,还是 TypeScript 更新必须独立完成?

Can TypeScript be updated automatically? TypeScript 可以自动更新吗?

VS Code ships with a recent stable version of TypeScript. VS Code 附带了最新的稳定版 TypeScript。

– from VS Code docs – 来自VS Code 文档

This means there's no way to automatically upgrade the TypeScript version used by VS Code.这意味着无法自动升级 VS Code 使用的 TypeScript 版本。 You can however override the TypeScript version VS Code uses by modifying either the user settings or the workspace settings.但是,您可以通过修改用户设置或工作区设置来覆盖 VS Code 使用的 TypeScript 版本。


What TypeScript version is VS Code using? VS Code 使用什么 TypeScript 版本?

When you open a TypeScript file, VS Code should display the TypeScript version in the status bar at the bottom right of the screen:当你打开一个 TypeScript 文件时,VS Code 应该会在屏幕右下角的状态栏中显示 TypeScript 版本:

VS Code 状态栏 TypeScript 版本


Changing the global TypeScript version更改全局 TypeScript 版本

  1. Install the desired TypeScript version globally, for example npm install -g typescript@2.0.5全局安装所需的 TypeScript 版本,例如npm install -g typescript@2.0.5
  2. Open VS Code User Settings ( F1 > Open User Settings )打开 VS Code 用户设置( F1 > Open User Settings
  3. Update/Insert "typescript.tsdk": "{your_global_npm_path}/typescript/lib" you can find out {your_global_npm_path} by typing npm root -g更新/插入"typescript.tsdk": "{your_global_npm_path}/typescript/lib"你可以通过输入 npm root -g 找到 {your_global_npm_path}

Now all of the projects you open with VS Code will use this TypeScript version, unless of course there is a workspace setting that overrides this.现在,您使用 VS Code 打开的所有项目都将使用此 TypeScript 版本,当然除非有工作区设置覆盖此版本。


Changing the local TypeScript version更改本地 TypeScript 版本

  1. Open the project in VS Code在 VS Code 中打开项目
  2. Install the desired TypeScript version locally, for example npm install --save-dev typescript@2.0.5在本地安装所需的 TypeScript 版本,例如npm install --save-dev typescript@2.0.5

    The --save-dev will update your project's package.json , adding the TypeScript version you installed as a devDependency . --save-dev将更新您项目的package.json ,添加您安装的 TypeScript 版本作为devDependency

  3. Open VS Code Workspace Settings ( F1 > Open Workspace Settings )打开 VS Code 工作区设置( F1 > Open Workspace Settings

  4. Update/Insert "typescript.tsdk": "./node_modules/typescript/lib"更新/插入"typescript.tsdk": "./node_modules/typescript/lib" / "typescript.tsdk": "./node_modules/typescript/lib" / "typescript.tsdk": "./node_modules/typescript/lib"

    Now only the project you installed this TypeScript version in will use that TypeScript version, the global installation will be ignored by VS Code in this project.现在只有你安装了这个 TypeScript 版本的项目才会使用那个 TypeScript 版本,这个项目中的全局安装将被 VS Code 忽略。

  5. Having added the typescript.tsdk entry it's then also necessary to use the VS Code UI to select the new version:添加typescript.tsdk条目后,还需要使用 VS Code UI 来选择新版本:

    • Click on the version displayed in the VS Code footer:单击 VS Code 页脚中显示的版本:

      vs 代码页脚

    • Select it in the UI:在 UI 中选择它:

      在 UI 中选择 ts 版本


See also:也可以看看:

Visual Studio Code comes with its own stable version of TypeScript but you can switch to a newer version as described in their docs Visual Studio Code 带有自己的稳定版本的 TypeScript,但您可以按照他们的文档中的描述切换到更新的版本

VS Code ships with a recent stable version of TypeScript. VS Code 附带了最新的稳定版 TypeScript。 If you want to use a newer version of TypeScript, you can define the typescript.tsdk setting ( File > Preferences > User/Workspace Settings ) pointing to a directory containing the TypeScript tsserver.js file.如果您想使用较新版本的 TypeScript,您可以定义typescript.tsdk设置( File > Preferences > User/Workspace Settings )指向包含 TypeScript tsserver.js文件的目录。
... ...
For example:例如:

 { "typescript.tsdk": "node_modules/typescript/lib" }

Is there a way to tell for sure whether Visual Studio Code is using version 2.0.3?有没有办法确定 Visual Studio Code 是否使用 2.0.3 版?

Open up a TypeScript file in Visual Studio Code and in the bottom right you will see the version of TypeScript it's using:在 Visual Studio Code 中打开一个 TypeScript 文件,在右下角你会看到它使用的 TypeScript 版本:

在此处输入图片说明

Is there a method for updating Visual Studio Code that will automatically update TypeScript to the latest released version, or does the TypeScript update have to be done independently?是否有更新 Visual Studio Code 的方法可以自动将 TypeScript 更新到最新发布的版本,还是必须独立完成 TypeScript 更新?

The way I've been doing it is to explicitly tell Visual Studio Code the folder where your TypeScript npm module is installed.我一直在这样做的方法是明确告诉 Visual Studio Code 安装 TypeScript npm 模块的文件夹。 I'm on Windows, so after you run the npm command to install TypeScript ( npm install -g typescript ) it will install it in this folder:我在 Windows 上,所以在你运行 npm 命令安装 TypeScript ( npm install -g typescript ) 后,它会安装在这个文件夹中:

C:\Users\username\AppData\Roaming\npm\node_modules\typescript\

So you need to tell Visual Studio Code to use the lib folder of your TypeScript npm install.所以你需要告诉 Visual Studio Code 使用你的 TypeScript npm 安装的lib文件夹。 You do this by:你这样做:

  1. Open VS Code settings (File -> Preferences -> Settings)打开 VS Code 设置(文件 -> 首选项 -> 设置)

  2. Search for typescript.tsdk setting搜索typescript.tsdk设置在此处输入图片说明

  3. Find where npm installed TypeScript with: npm list -g typescript .使用以下命令查找 npm 安装 TypeScript 的位置: npm list -g typescript In my case, it returned C:\\Users\\username\\AppData\\Roaming\\npm就我而言,它返回C:\\Users\\username\\AppData\\Roaming\\npm

  4. Override the value of typescript.tsdk setting to: C:\\\\Users\\\\username\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\typescript\\\\lib Note the use of double backward slashes to have a properly escaped string with backward slashes.typescript.tsdk设置的值覆盖为: C:\\\\Users\\\\username\\\\AppData\\\\Roaming\\\\npm\\\\node_modules\\\\typescript\\\\lib注意使用双反斜杠可以正确转义字符串反斜杠。

  5. Confirm that VS Code is using the npm version of TypeScript for intellisense by opening a TypeScript file, clicking the TypeScript version number in the bottom right and seeing in the task window that VS Code is loading TypeScript from the directory specified in step 4:通过打开 TypeScript 文件,单击右下角的 TypeScript 版本号并在任务窗口中看到 VS Code 正在从步骤 4 中指定的目录加载 TypeScript,确认 VS Code 正在使用 npm 版本的 TypeScript 进行智能感知:

在此处输入图片说明

  1. Confirm that VS Code is using the correct version of TypeScript for compiling by going to this folder and changing the name of the file:通过转到此文件夹并更改文件名,确认 VS Code 正在使用正确版本的 TypeScript 进行编译:

C:\\Users\\username\\AppData\\Roaming\\npm\\tsc.cmd (to something like tsc1.cmd) C:\\Users\\username\\AppData\\Roaming\\npm\\tsc.cmd(类似于 tsc1.cmd)

Now try building in VS Code (Tasks -> Run Tasks -> tsc:build - tsconfig.json) and you should get this error message in the VS Code terminal window:现在尝试在 VS Code 中构建(任务 -> 运行任务 -> tsc:build - tsconfig.json),你应该会在 VS Code 终端窗口中收到此错误消息:

'tsc' is not recognized as an internal or external command, operable program or batch file.
The terminal process terminated with exit code: 1
  1. Change the file back to tsc.cmd and you should now be able to build and have Intellisense in VS Code for the globally installed TypeScript node package将文件改回 tsc.cmd,您现在应该能够在 VS Code 中为全局安装的 TypeScript 节点包构建并拥有 Intellisense

To automatically use the Typescript version installed in your workspace's node_modules , without having to configure it every time you set up a new workspace, you can set the default Typescript setting in the User Settings JSON ( not Workspace ) to use a relative path:要自动使用安装在您工作区的node_modules的 Typescript 版本,而不必在每次设置新工作区时进行配置,您可以在用户设置 JSON而不是 Workspace )中设置默认的 Typescript 设置以使用相对路径:

{
    // ... other User settings
    "typescript.tsdk": "./node_modules/typescript/lib"
}

Now, when you run the "Select Typescript Version..." command, the "VS Code's Version" will always be the same as the "Workspace Version":现在,当您运行“Select Typescript Version...”命令时,“VS Code 的版本”将始终与“工作区版本”相同:

'选择打字稿版本...'命令

The only potential downside to this is that it means you always need Typescript installed in the workspace you're working in. Though, if you're writing Typescript anywhere, I think that's a reasonable expectation.唯一潜在的缺点是这意味着您始终需要在您工作的工作区中安装 Typescript。不过,如果您在任何地方编写 Typescript,我认为这是一个合理的期望。

You should see a version number listed on the bottom bar:您应该会在底部栏中看到一个版本号:

在此处输入图片说明

If you click on the number (2.4.0 above) you will be presented with an option to choose the version you would like to use:如果您单击数字(上面的 2.4.0),您将看到一个选项来选择您要使用的版本:

在此处输入图片说明

If you don't see the version you want that means it probably isn't installed and you have to install it.如果你没有看到你想要的版本,那意味着它可能没有安装,你必须安装它。

npm install -g typescript@2.7.2

Replace 2.7.2 with the version you want to install.2.7.2替换为您要安装的版本。

Though I could not find the file tsserver.js when I used Spotlight on my Mac, I tried again using mdfind, and I found its location to be "/usr/local/lib/node_modules/typescript/lib/"虽然我在 Mac 上使用 Spotlight 时找不到文件 tsserver.js,但我再次尝试使用 mdfind,我发现它的位置是“/usr/local/lib/node_modules/typescript/lib/”

So I used that path when editing my Workspace settings in settings.json所以我在 settings.json 中编辑我的工作区设置时使用了该路径

Now I am using my most recent version of TypeScript, and VSCode tells me that I am using my most recent version.现在我使用的是最新版本的 TypeScript,VSCode 告诉我我使用的是最新版本。

我建议安装JavaScript 和 TypeScript Nightly 扩展,这将使 VS Code 的行为就像它的内置版本是来自 npm 的当前typescript@next

I have had a similar problem and now I succeeded in having an up-to-date version of TypeScript by modifying the environment variables on my system.我遇到了类似的问题,现在我通过修改系统上的环境变量成功地获得了最新版本的 TypeScript。 In my case, we need a TypeScript version higher than 2. But I was only able to use version 1.8.3.就我而言,我们需要一个高于 2 的 TypeScript 版本。但我只能使用 1.8.3 版本。 First thing is I went to my system environment variables and checked the path variable.第一件事是我去我的系统环境变量并检查了路径变量。 In there I found a reference to TypeScript version 1.8.3.在那里我找到了对 TypeScript 1.8.3 版的引用。

C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\1.8.3\\ C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\1.8.3\\

When I checked the parent directory, this was also the highest version installed in that directory.当我检查父目录时,这也是该目录中安装的最高版本。 I would have thought by globally installing the latest version, that I would see this here too but it is not there.我本以为通过全局安装最新版本,我也会在这里看到这个,但它不在那里。 The version you see here is the one that got installed with Visual Studio (not visual studio code).您在此处看到的版本是随 Visual Studio 一起安装的版本(不是 Visual Studio 代码)。

So I went to Visual Studio and updated the TypeScript library to the latest version via Options > Extensions and Updates.所以我去了 Visual Studio 并通过选项 > 扩展和更新将 TypeScript 库更新到最新版本。 There I searched for the latest version of TypeScript and installed it.在那里我搜索了最新版本的 TypeScript 并安装了它。

This made a new version available in the parent directory mentioned earlier.这使得前面提到的父目录中的新版本可用。 I then changed the path variable to:然后我将路径变量更改为:

C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\2.2\\ C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\2.2\\

When I now open VS Code and type tsc -v I see that I am using the latest version.当我现在打开 VS Code 并输入 tsc -v 时,我看到我使用的是最新版本。 No mismatch message any more, etc. Hope this helps you guys out a bit.不再有不匹配的消息等。希望这对你们有所帮助。

Typescript package has a compiler and a language service. Typescript 包有一个编译器和一个语言服务。 VScode comes with a Typescript language service, not with the compiler. VScode 附带了 Typescript 语言服务,而不是编译器。 You can see the language service in the bottom right, like the other answers show, but you can't see what compiler version is been used.您可以在右下角看到语言服务,就像其他答案显示的那样,但您看不到使用的编译器版本。

You can have different versions for the compiler and the language service.编译器和语言服务可以有不同的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM