我正在使用 TypeScript 运行 express.js 应用程序。每次我尝试处理时request.query.foo都会收到以下错误:
Argument of type 'string | ParsedQs | string[] | ParsedQs[] | undefined' is not assignable to parameter of type 'string'.
Run Code Online (Sandbox Code Playgroud)
设置:
import { Request, Response } from 'express';
function bar(request: Request, response: Response) {
const foo: string = request.query.foo; //marked as error
}
Run Code Online (Sandbox Code Playgroud)
我在 Express 的文档中读到,您可以设置一个名为“查询解析器”的配置,当设置为“简单”时,它将始终将查询参数解析为字符串
问题是Typescript仍然认为可能会出现除字符串或未定义之外的其他内容,而我似乎找不到覆盖Request接口的方法,我只能扩展它。
Is there any way to override the Request interface? is there something wrong in my approach?