Typescript 如何使用express在回调函数之间传递值?
我正在尝试文档中附带的方法,以按特定顺序进行函数调用链,因此我希望将值从Typescript 如何使用express在回调函数之间传递值?,typescript,express,Typescript,Express,我正在尝试文档中附带的方法,以按特定顺序进行函数调用链,因此我希望将值从cb0传递到cb1(或cb2),目前我正在req对象中设置一个属性,并从另一个处理程序访问它,这一切正常 const express = require('express'); const app = express(); const PORT = 8000; const cb0 = function (req, res, next) { console.log('CB0'); req.cb0val = 'Hell
cb0
传递到cb1
(或cb2
),目前我正在req
对象中设置一个属性,并从另一个处理程序访问它,这一切正常
const express = require('express');
const app = express();
const PORT = 8000;
const cb0 = function (req, res, next) {
console.log('CB0');
req.cb0val = 'Hello';
next();
}
const cb1 = function (req, res, next) {
console.log('CB1');
req.cb1val = 'World';
next();
}
const cb2 = function (req, res) {
res.send(`Hey, ${req.cb0val} ${req.cb1val}`);
}
app.get('/', [cb0, cb1, cb2])
app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});
使用typescript
import express from 'express';
const app = express();
const PORT = 8000;
const cb0: express.RequestHandler = function (req: express.Request, res: express.Response, next: Function) {
console.log('CB0');
req.cb0val = 'Hello';
next();
}
const cb1: express.RequestHandler = function (req: express.Request, res: express.Response, next: Function) {
console.log('CB1');
req.cb1val = 'World';
next();
}
const cb2: express.RequestHandler = function (req: express.Request, res: express.Response) {
res.send(`Hey, ${req.cb0val} ${req.cb1val}`);
}
app.get('/example/c', [cb0, cb1, cb2])
app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});
因为我正在将req
的类型设置为express.Request
我无法设置该类型的新属性,因此出现以下错误:
index.ts:7:7 - error TS2339: Property 'cb0val' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
7 req.cb0val = 'Hello';
~~~~~~
index.ts:13:7 - error TS2339: Property 'cb1val' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
13 req.cb1val = 'World';
~~~~~~
index.ts:18:24 - error TS2339: Property 'cb0val' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
18 res.send(`Hey, ${req.cb0val} ${req.cb1val}`);
~~~~~~
index.ts:18:38 - error TS2339: Property 'cb1val' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
18 res.send(`Hey, ${req.cb0val} ${req.cb1val}`);
~~~~~~
index.ts:7:7-错误TS2339:类型“Request”上不存在属性“cb0val”。
7 req.cb0val='你好';
~~~~~~
index.ts:13:7-错误TS2339:类型“Request”上不存在属性“cb1val”。
13 req.cb1val=‘世界’;
~~~~~~
index.ts:18:24-错误TS2339:类型“Request”上不存在属性“cb0val”。
18 res.send(`Hey,${req.cb0val}${req.cb1val}`);
~~~~~~
index.ts:18:38-错误TS2339:类型“Request”上不存在属性“cb1val”。
18 res.send(`Hey,${req.cb0val}${req.cb1val}`);
~~~~~~
在不将
express.Request
的类型更改为any
的情况下,处理此场景的正确方法是什么?您可以使用一种称为声明合并的方法
在项目中的某个地方创建一个名为express.d.ts
的文件。这通常是在项目根目录中的@types
文件夹中创建的(@types/express.d.ts
)
此文件的内容应为
declare namespace Express {
interface Request {
cb0val: string
// other custom properties ...
}
}
在tsconfig中,设置typeRoot
或将新文件添加到types
字段
"typeRoots": [
"@types/",
"node_modules/@types/"
]