我正在尝试为interface创建一个新的扩展express.RequestHandler,但是这个错误似乎出现了。我不明白为什么。
An interface can only extend an identifier/qualified-name with optional type arguments. ts(2499)express.RequestHandler接口不支持异步函数。上面写着
The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>>'?ts(1064)这是我的接口
export interface IRequest extends express.Request {
user: IUser;
}
export interface IRequestHandler extends RequestHandler = (
req: IRequest,
res: express.Response,
next: express.NextFunction
) => void | Promise<void>;发布于 2021-12-28 06:13:49
您似乎正在尝试扩展express.Request接口。
尝试:
import { NextFunction, Request, Response } from 'express';
interface IUser {
name: string;
}
declare module 'express' {
interface Request {
user: IUser;
}
}
const controller = async (req: Request, res: Response, next: NextFunction) => {
console.log(req.user);
};

https://stackoverflow.com/questions/70486812
复制相似问题