打字稿:映射类型中的枚举键

时间:2018-03-01 08:21:15

标签: typescript generics enums definition

我有一个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的成员吗?

1 个答案:

答案 0 :(得分:4)

您的问题主要在于:type Route<M extends Methods = any>

首先,默认值any会导致M中的string类型为RouteMethodValidator,因为Route<any>['methods']anykeyof anystring

现在,将默认值更改为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>;
}