我正在嘗試創建一個新的擴展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>;
uj5u.com熱心網友回復:
看來您正在嘗試擴展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://www.uj5u.com/houduan/396772.html
標籤:javascript 打字稿 表达
