首页 文章

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

提问于
浏览
62

如何判断Visual Studio代码中使用的TypeScript版本?特别是,我一直在使用TypeScript 1.8.10和VSCode 1.4.0 . 我首先将VSCode更新到最新版本,即1.5.3 . 但是从命令行检查,我看到我的TypeScript版本仍然是1.8.10 . 所以我从命令行更新了TypeScript,它现在是2.0.3 .

有没有办法确定Visual Studio Code是否使用版本2.0.3?

是否有更新Visual Studio代码的方法会自动将TypeScript更新为最新发布的版本,还是必须单独完成TypeScript更新?

7 回答

  • 101

    来自https://code.visualstudio.com/docs/languages/typescript#_using-newer-typescript-versions

    VS Code附带了最近稳定版的TypeScript .

    这意味着无法自动升级VS Code使用的TypeScript版本 . 但是,您可以通过修改用户设置或工作区设置来覆盖VS Code使用的TypeScript版本 .

    VS Code使用什么TS版本?

    当您打开TypeScript文件时,VS Code应在屏幕右下方的状态栏中显示TypeScript版本:

    VS Code status bar TypeScript version

    更改全局TypeScript版本

    • 全局安装所需的TypeScript版本,例如 npm install -g typescript@2.0.5

    • 打开VS代码用户设置(F1> Open User Settings

    • 更新/插入 "typescript.tsdk": "{your_global_npm_path}/typescript/lib" 您可以通过键入npm root -g找到

    现在,使用VS Code打开的所有项目都将使用此TypeScript版本,除非当然有一个工作空间设置会覆盖它 .

    更改本地TypeScript版本

    • 在VS Code中打开项目

    • 在本地安装所需的TypeScript版本,例如 npm install --save-dev typescript@2.0.5

    • 打开VS代码工作区设置(F1> Open Workspace Settings

    • 更新/插入 "typescript.tsdk": "./node_modules/typescript/lib"

    现在,只有您安装此TypeScript版本的项目才会使用该TypeScript版本,此项目中的VS Code将忽略全局安装 .

    Note: --save-dev 将更新项目的 package.json ,将您安装的TypeScript版本添加为 devDependency .

  • 0

    Visual Studio Code附带了自己的TypeScript稳定版本,但您可以按照docs中的说明切换到更新的版本

    VS Code附带了最近稳定版的TypeScript . 如果要使用较新版本的TypeScript,可以定义指向包含TypeScript tsserver.js文件的目录的typescript.tsdk设置(“文件”>“首选项”>“用户/工作区设置”) . ... 例如: {
    “typescript.tsdk”:“node_modules / typescript / lib”
    }

  • 1

    Is there a way to tell for sure whether Visual Studio Code is using version 2.0.3?

    在Visual Studio Code中打开TypeScript文件,在右下角,您将看到它正在使用的TypeScript版本:

    enter image description here

    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?

    我在Windows上的方式是've been doing it is to explicitly tell Visual Studio Code the folder where your TypeScript npm module is installed. I'm,所以在运行npm命令安装TypeScript( npm install -g typescript )后,它会将它安装在这个文件夹中:

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

    因此,您需要告诉Visual Studio Code使用TypeScript npm install的 lib 文件夹 . 你这样做:

    • 打开VS代码设置(文件 - >首选项 - >设置)

    • 搜索 typescript.tsdk 设置
      enter image description here

    • 找到npm安装TypeScript的地方: npm list -g typescript . 在我的情况下,它返回 C:\Users\username\AppData\Roaming\npm

    • typescript.tsdk 设置的值覆盖为: 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文件确认VS Code正在使用npm版本的TypeScript进行智能感知,单击右下角的TypeScript版本号,然后在任务窗口中看到VS Code从步骤4中指定的目录加载TypeScript:

    enter image description here

    • 通过转到此文件夹并更改文件名,确认VS Code正在使用正确版本的TypeScript进行编译:

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

    现在尝试在VS代码中构建(任务 - >运行任务 - > 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
    
    • 将文件更改回tsc.cmd,您现在应该能够在全局安装的TypeScript节点包的VS Code中构建并使用Intellisense
  • 12

    您应该会看到底栏上列出的版本号:

    enter image description here

    如果您点击数字(上面的2.4.0),您将看到一个选项,选择您想要使用的版本:

    enter image description here

    如果您没有看到所需的版本这意味着它可能没有安装,你必须安装它 .

    npm install -g typescript@2.7.2
    

    2.7.2 替换为您要安装的版本 .

  • 0

    虽然我在Mac上使用Spotlight时找不到文件tsserver.js,但我再次尝试使用mdfind,我发现它的位置是“/ usr / local / lib / node_modules / typescript / lib /”

    所以我在settings.json中编辑我的Workspace设置时使用了该路径

    现在我正在使用我最新版本的TypeScript,VSCode告诉我我正在使用我最新版本 .

  • 0

    我遇到了类似的问题,现在我通过修改系统上的环境变量成功地获得了最新版本的TypeScript . 在我的情况下,我们需要一个高于2的TypeScript版本 . 但我只能使用版本1.8.3 . 首先,我去了我的系统环境变量并检查了路径变量 . 在那里,我找到了TypeScript版本1.8.3的引用 .

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

    当我检查父目录时,这也是该目录中安装的最高版本 . 我想通过全局安装最新版本,我也会在这里看到它,但它不存在 . 您在此处看到的版本是使用Visual Studio安装的版本(不是Visual Studio代码) .

    所以我去了Visual Studio并通过Options> Extensions and Updates将TypeScript库更新到最新版本 . 在那里,我搜索了最新版本的TypeScript并安装了它 .

    这使得前面提到的父目录中提供了新版本 . 然后我将路径变量更改为:

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

    当我现在打开VS Code并输入tsc -v时,我发现我使用的是最新版本 . 没有不匹配的消息,等等 . 希望这可以帮助你们一点点 .

  • 9

    Typescript包有一个编译器和一个语言服务 . VScode附带一个Typescript语言服务,而不是编译器 . 您可以在右下角看到语言服务,就像其他答案所示,但您无法看到使用了哪个编译器版本 .

    您可以为编译器和语言服务提供不同的版本 .

相关问题