简体   繁体   English

VSCode 工作,但 TS 编译器抛出错误:VSCode 和 TS 编译器不同步

[英]VSCode works but TS compiler throws error: VSCode and TS compiler are not in sync

I write my project code in VSCode, I have been writing Node.js Express application.我在VSCode中编写我的项目代码,我一直在编写Node.js Express应用程序。 I need to access req.user.name from Request object, VSCode complained that name does not exist in user object.我需要从请求 object 中访问req.user.name ,VSCode 抱怨user object 中不存在该名称。 So I created global.d.ts file in my project root directory which has the following content:所以我在我的项目根目录中创建了global.d.ts文件,该文件具有以下内容:

declare namespace Express {
  interface User {
    userName: string;
    name: string;
    email: string;
  }
}

In my tsconfig.json, I have referenced this file as follows:在我的 tsconfig.json 中,我引用了这个文件如下:

{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types", "./global.d.ts"]
  },
  "include": [/*...*/,"global.d.ts"]
}

After this, VSCode stopped complaining me about the error above, but when I try to compile the project with actual tsc , it throws error.在此之后,VSCode 不再抱怨我上面的错误,但是当我尝试使用实际的tsc编译项目时,它会抛出错误。 Error from the compiler: error TS2339: Property 'name' does not exist on type 'User' .来自编译器的错误: error TS2339: Property 'name' does not exist on type 'User'

For some reason, VSCode and TS compiler are not in sync.由于某种原因,VSCode 和 TS 编译器不同步。

How can I resolve this?我该如何解决这个问题?

VS Code does not automatically use your project's TS compiler for its TS language services. VS Code 不会自动将项目的 TS 编译器用于其 TS 语言服务。 It comes pre-installed with a version of TSC which it will use unless you tell it otherwise, so can become out of sync.它预装了一个 TSC 版本,除非您另有说明,否则它将使用该版本,因此可能会变得不同步。

In order to ensure your IDE is in sync with your project's compiler version, you can tell it where to find your project's TSC binary in the current workspace为了确保您的 IDE 与您项目的编译器版本同步,您可以告诉它在当前工作区中的何处可以找到您的项目的 TSC 二进制文件

If you haven't already, create a .vscode directory at root level of your project, and add a settings.json to it, here you can configure VS Code with project specific settings.如果你还没有,在你的项目的根目录创建一个.vscode目录,并添加一个settings.json到它,在这里你可以使用项目特定的设置来配置 VS Code。

Typically if TSC is installed as a root level node module you would add this to your .vscode/settings.json通常,如果 TSC 作为根级节点模块安装,您会将其添加到您的.vscode/settings.json

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

There are some GUI based approaches to the above that essentially achieve the same thing, for more info check out these docs...上面有一些基于 GUI 的方法基本上可以实现相同的目标,有关更多信息,请查看这些文档...

https://code.visualstudio.com/docs/typescript/typescript-compiling#_compiler-versus-language-service https://code.visualstudio.com/docs/typescript/typescript-compiling#_compiler-versus-language-service

https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript

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

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