当我提出明确请求时,我收到此错误。
error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
这是我扩展请求的测试代码。
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }),
function(req: Request, res: Response) {
req.test = 'aa'
return console.log(req.test)
});
为了解决这个问题,我已经搜索了很多并且我做到了。
Make types/express/index.d.ts
文件。我添加 typeRoots 于tsconfig.json
像下面这样。
"typeRoots": ["./node_modules/@types", "./types"],
这是 types/express/index.d.ts 文件,我尝试了什么。
declare namespace Express {
export interface Request {
test: string
}
}
// This is also fail
declare namespace Express {
interface Request {
test: string
}
}
// This is also fail
declare global {
namespace Express {
interface Request {
test: string
}
}
}
Even my vscode
tell me req.test
type is string, server show me error.
这是控制台上的完整错误
[nodemon] starting `ts-node src/app.ts`
/app/node_modules/ts-node/src/index.ts:434
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts(67,9): error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
src/app.ts(68,28): error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
at createTSError (/app/node_modules/ts-node/src/index.ts:434:12)
at reportTSError (/app/node_modules/ts-node/src/index.ts:438:19)
at getOutput (/app/node_modules/ts-node/src/index.ts:578:36)
at Object.compile (/app/node_modules/ts-node/src/index.ts:775:32)
at Module.m._compile (/app/node_modules/ts-node/src/index.ts:858:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
这是我的 package.json,你可以看到我的 typescript 版本是 3.9.6
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon --exec ts-node src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.1",
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.4",
"passport": "^0.4.1",
"passport-google-oauth20": "^2.0.0",
"ts-node": "^8.10.2"
},
"devDependencies": {
"@types/body-parser": "^1.19.0",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.7",
"@types/express-session": "^1.17.0",
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^14.0.18",
"@types/passport": "^1.0.4",
"@types/passport-google-oauth20": "^2.0.3",
"typescript": "^3.9.6"
}
}
这是我所有的 tsconfig.json
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"removeComments": true, /* Do not emit comments to output. */
"strict": true, /* Enable all strict type-checking options. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"typeRoots": ["./node_modules/@types", "./types"], /* List of folders to include type definitions from. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": [
"nod_modules",
"**/*.spec.ts"
]
}
我花了时间并从昨天开始感到压力。我该如何解决它?