【发布时间】:2022-01-12 20:18:33
【问题描述】:
我在 VSCode 中编写我的项目代码,我一直在编写 Node.js Express 应用程序。我需要从 Request 对象访问 req.user.name,VSCode 抱怨 user 对象中不存在名称。所以我在我的项目根目录中创建了global.d.ts 文件,其内容如下:
declare namespace Express {
interface User {
userName: string;
name: string;
email: string;
}
}
在我的 tsconfig.json 中,我引用了这个文件如下:
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./global.d.ts"]
},
"include": [/*...*/,"global.d.ts"]
}
在此之后,VSCode 不再抱怨我上面的错误,但是当我尝试使用实际的 tsc 编译项目时,它会抛出错误。
来自编译器的错误:error TS2339: Property 'name' does not exist on type 'User'。
由于某种原因,VSCode 和 TS 编译器不同步。
我该如何解决这个问题?
【问题讨论】:
标签: node.js typescript express visual-studio-code