【发布时间】:2020-05-26 16:16:52
【问题描述】:
我正在尝试为我的变量分配一个类型,该类型从 express 的请求查询中获取其值。 req.query 的类型为“QueryString.ParsedQs” 例如 req.query.accountId 的类型为“string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[]”
如何从 req.query.accountId 中提取值,使其始终为字符串类型?
示例代码:
import { Request, Response } from "@feathersjs/express";
class Xyz {
public myMethod( req: Request, res: Response ){
const accountId =req.query.accountId;/* type is "string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[]"*/
}
}
【问题讨论】:
-
req.query.accountId as string? -
是的,我想将 accountId 传递给只接受字符串的函数。光说它“作为字符串”,会不会变成字符串?
-
...就是这样。见typescriptlang.org/docs/handbook/…
-
嗯,我的意思是它应该可以工作,只要它不是 ParsedQs,我不确定什么时候会发生。谢谢!
标签: typescript express request