【发布时间】:2021-06-16 18:10:25
【问题描述】:
interface Info<
BodyType = undefined,
QueryStringType = undefined,
PathParamType = undefined,
ResponseType = undefined
> {
request: {
queryStrings: QueryStringType;
pathParameters: PathParamType;
body: BodyType;
};
response: ResponseType;
}
type InfoExtend = Info<{ name: string }>;
const a1: InfoExtend = {
request: {
body: { name: "a" },
},
};
我有一个Info 类型,它是描述端点的接口,InfoExtend 是端点实现的实际类型。
问题是,a1 有一个类型错误:
类型'{正文:{名称:字符串; }; }' 缺少来自类型 '{ queryStrings: undefined; 的以下属性。路径参数:未定义;正文:{名称:字符串; }; }':查询字符串,路径参数(2739)
我明白了,因为 queryStrings 和 pathParameters。不是可选的
问题是,我能否将body、queryStrings 和pathParameters 与泛型关联,这样,如果我通过BodyType 泛型,那么body 在Info.request.body 中,如果我不通过BodyType,那么Info.request应该没有任何错误,并且必须包含3个中的1个(body/queryString/pathParams)
【问题讨论】:
标签: typescript