问题是 vscode 智能感知未显示使用 typeRoots
中的 tsconfig.json
声明性合并描述的类型。
但是这可以正常编译,并且mocha 测试也可以正常运行。测试脚本是 "test": "NODE_ENV=test mocha --check-leaks -r ts-node/register -r dotenv/config \"./test/**/*.test.ts\"",
这是文件夹结构
|-src/
|--@types/
|----express/
|------index.d.ts
|--middleware/
|----isAuth.ts
|-test/
|--isAuth.test.ts
|-tsconfig.json
src/@types/express/index.d.ts
的内容是
import 'express';
import type { Mongoose } from 'mongoose';
declare global {
namespace Express {
interface Request {
userId?: Mongoose.ObjectId;
isAuth: boolean;
}
}
}
tsconfig.json
已关注
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"noUnusedLocals": true,
"moduleResolution": "node",
"baseUrl": "./src",
"typeRoots": ["./src/@types", "./node_modules/@types"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules", "dist", "test"]
}
src/middleware/isAuth.ts
中的 Intellisense 工作并显示添加的 isAuth
和“userId”字段,如此图中所示
intellisense in src/middleware/isAuth.ts
但它不起作用 test/isAuth.test.ts
不是 included
中的 tsconfig.json
。
tooltip in test/isAuth.test.ts
vscode error in test/isAuth.test.ts
如果我从 "test"
中删除 "exclude"
,这些错误已解决,但我收到此错误
File '/.../test/isAuth.test.ts' is not under 'rootDir' '/.../src'. 'rootDir' is expected to contain all source files.
The file is in the program because:
Matched by include pattern '**/*' in '/.../tsconfig.json'
当我评论 rootDir
来解决这个问题时,src/
和 test/
都会被编译,我不想编译 test/
。