在打字稿中扩展快速请求类型
extends express request type in typescript
重复:
import { Router } from 'express';
import * as uuid from 'uuid';
我正在使用打字稿 3.6.4。我正在扩展 express.Request 类型,因为我想添加 id 字段:
interface request extends Express.Request {
id: string
}
如果我使用设置 id 的中间件创建 post 方法:
router.post('/orders', (req: request, _res: express.Response, next) => {req.id = uuid.v1(); next();}, async function (req: request, res: express.Response) {
...
});
但是我在请求中遇到错误:request
tscongif.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es5",
"es6",
"dom",
"es2017",
"esnext.asynciterable"
],
"strict": true,
"alwaysStrict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": "./src",
"esModuleInterop": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"sourceMap": true,
"outDir": "./dist",
"removeComments": true
},
"exclude": ["node_modules"],
"include": [
"./src/**/*", "./src/**/*.tsx", "./src/**/*.ts"
]
}
我想你正在寻找 declaration merging
您可以合并界面。
在 global.d.ts 中声明您的界面,ts 将合并它。
declare global {
declare module 'express' {
export interface Request {
id: string;
}
}
}
而且您可以在应用中的任何地方使用它
const id = Express.request.id;
const anythingElseFromRequestInterface = Express.request.baseUrl;
这个对我有用!
declare module "express" {
interface Request {
id: string
}
}
重复:
import { Router } from 'express';
import * as uuid from 'uuid';
我正在使用打字稿 3.6.4。我正在扩展 express.Request 类型,因为我想添加 id 字段:
interface request extends Express.Request {
id: string
}
如果我使用设置 id 的中间件创建 post 方法:
router.post('/orders', (req: request, _res: express.Response, next) => {req.id = uuid.v1(); next();}, async function (req: request, res: express.Response) {
...
});
但是我在请求中遇到错误:request
tscongif.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es5",
"es6",
"dom",
"es2017",
"esnext.asynciterable"
],
"strict": true,
"alwaysStrict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": "./src",
"esModuleInterop": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"sourceMap": true,
"outDir": "./dist",
"removeComments": true
},
"exclude": ["node_modules"],
"include": [
"./src/**/*", "./src/**/*.tsx", "./src/**/*.ts"
]
}
我想你正在寻找 declaration merging
您可以合并界面。 在 global.d.ts 中声明您的界面,ts 将合并它。
declare global {
declare module 'express' {
export interface Request {
id: string;
}
}
}
而且您可以在应用中的任何地方使用它
const id = Express.request.id;
const anythingElseFromRequestInterface = Express.request.baseUrl;
这个对我有用!
declare module "express" {
interface Request {
id: string
}
}