我有一个http方法的枚举:
export enum HttpMethod {
GET = 'GET', POST = 'POST', /*...*/
}
然后我定义了一个基本的方法类型,它可以有任何HttpMethod
作为键:
type Methods = {
[M in HttpMethod]?: any;
};
基本路线类型可以使用此方法类型:
type Route<M extends Methods = any> = {
methods: M;
}
所以我可以定义任何路线,如:
interface AnyRoute extends Route<{
[HttpMethod.GET]: AnyRequestHandler;
}> {}
到目前为止一切顺利。现在我要添加Validator
:
type Validator<R extends Route, M extends HttpMethod> = {/*...*/}
并且只想允许将Method
添加到Validator
中定义的Route
:
type RouteMethodValidators<R extends Route> = {
[M in keyof R['methods']]?: Validator<R, M>;
};
虽然我的IDE似乎理解它,但我收到以下错误:
Type 'M' does not satisfy the constrain 'HttpMethod'.
Type 'keyof R["methods"]' is not assignable to type 'HttpMethod'.
有什么方法可以告诉打字稿,这绝对是HttpMethod
的成员吗?
答案 0 :(得分:4)
您的问题主要在于:type Route<M extends Methods = any>
首先,默认值any
会导致M
中的string
类型为RouteMethodValidator
,因为Route<any>['methods']
为any
且keyof any
为string
。
现在,将默认值更改为Methods
仍然无法解决问题,因为您执行M extends Methods
这基本上意味着M
可以拥有比Methods
中定义的更多的键。 1}},即比HttpMethods
中定义的更多。但在Validator
中,您只允许HttpMethods
的值。
我认为您最好的选择是让Route
不是通用的。
type Route = {
methods: Methods;
}
type RouteMethodValidators<R extends Route> = {
[M in HttpMethod]?: Validator<R, M>;
}