简体   繁体   English

Typescript function arguments 基于以枚举为键的类型

[英]Typescript function arguments based on types with enums as keys

Lets say that I have an enum with a set of action types:假设我有一个带有一组动作类型的枚举:

export enum ActionTypes {
  action1 = 'action1',
  action2 = 'action2',
  action3 = 'action3',
  // ...etc
}

And I have a set of payload types that a function wants when performing that action, which is keyed off the action types themselves:我有一组有效负载类型,function 在执行该操作时需要这些类型,这些类型与操作类型本身无关:

export type ActionPayloads = {
  [ActionTypes.action1]: boolean,
  [ActionTypes.action2]: string,
  [ActionTypes.action3]: void,
  // ...etc
}

I want to use a function that takes as its first parameter the action type from the enum, and then use TS hinting to have it infer the payload type for argument two:我想使用一个 function 作为它的第一个参数来自枚举的动作类型,然后使用 TS 提示让它推断参数二的有效负载类型:

function exampleFn<T extends ActionTypes, U extends ActionPayloads[T]> (type: T, data?: U): void {
  //...
}

I feel like this would work, but even if I change ActionPayloads to an interface, in both cases for U extends ActionPayloads[T] I get the error TS2536: Type 'T' cannot be used to index type 'ActionPayloads'我觉得这可行,但即使我将 ActionPayloads 更改为接口,在这两种情况下U extends ActionPayloads[T]我都会收到错误TS2536: Type 'T' cannot be used to index type 'ActionPayloads'

Am I missing something here?我在这里错过了什么吗? I feel like this should work, given that there's nothing fancy going on with the usage of the enum.我觉得这应该可行,因为枚举的使用没有什么特别的。

Updated to reproduce the error更新以重现错误

The error can be reproduced by omitting any possibility of ActionTypes from ActionPayloads .可以通过从ActionPayloads中省略任何可能的ActionTypes来重现该错误。

export enum ActionTypes {
  action1 = "action1",
  action2 = "action2",
  action3 = "action3",
  // ...etc
}

export type ActionPayloads = {
  [ActionTypes.action1]: boolean;
  // [ActionTypes.action2]: string; // accidentally omit action2
  [ActionTypes.action3]: void;
  // ...etc
}

function exampleFn<T extends ActionTypes, U extends ActionPayloads[T]>(type: T, data?: U): void {

}

Since exampleFn takes all possibilities of ActionTypes as T , U expands ActionPayloads[T] with such.由于exampleFnActionTypes的所有可能性视为T ,因此U用此类扩展ActionPayloads[T] If any possibility of T is omitted in ActionPayloads , T cannot be used to index ActionPayloads .如果在ActionPayloads中省略了T的任何可能性,则T不能用于索引ActionPayloads That's what the error message tells.这就是错误消息所说的。

Okay I figured it out.好吧,我想通了。 My enum, which is quite a bit larger than the example, had one entry that was not accounted for in the payload type.我的枚举比示例大很多,其中有一个条目未在有效负载类型中考虑。

This entire thing was caused by a mismatch;这整件事是由不匹配引起的; it works fine.它工作正常。

Long story short, the error message does not really provide information of use, and make sure that your keys from both enum and type line up.长话短说,错误消息并没有真正提供使用信息,并确保您的 enum 和 type 中的密钥对齐。 Huge waste of time here.在这里浪费了很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM