使用通用类型扩展/修改express.Request

时间:2019-07-31 13:32:46

标签: node.js typescript express request

我正在尝试扩展/修改express.Request接口,但收到的错误是我的接口在应用时缺少很多属性。

interface User extends Request {
    currentUser: User; // basic properties of an user
}
interface IGetUserRequest {
    query: {
        offset: number;
    }
}

type TypedRequest<T> =
    Omit<Request, 'query'>
    & T
    & User;

...

router.get('/route', async (req: TypedRequest<IGetUserRequest>, res) => {
    // access req.query.offset as number instead of "any"
});


我的目标是将req.query.offset属性描述为“数字”而不是“任何”,但是我却得到了:

Argument of type '(req: TypedRequest<IGetUserRequest>, res: Response) =>
Promise<Response>' is not assignable to parameter of type 'Application'.

Type '(req: TypedRequest<IGetUserRequest>, res: Response) => Promise<Response>'
is missing the following properties from type 'Application':
init, defaultConfiguration, engine, set, and 62 more.

0 个答案:

没有答案