简体   繁体   English

Typescript 通用接口阵列

[英]Typescript Generic Array of Interfaces

I have a problem with more advanced Generic types in TypeScript.我对 TypeScript 中更高级的泛型类型有疑问。

What I want to do is an array of data where the data itself controlled by the type like:我想要做的是一个数据数组,其中data本身由以下type控制:

[
 {
  type: "T1",
  content: number,
 },
 {
  type: "T2",
  values: {
   id: number,
   ...
  }
 }
]

I kinda get stacked after describing the available types like:在描述了可用类型后,我有点堆积如山:

enum DataType {
 T1 = "t1",
 T2 = "t2"
}

So the result must look kinda like this, I guess:所以结果一定是这样的,我猜:

interface DataGeneric<T> {
 [T extends DataType.T1]: { content: number },
 [T extends DataType.T2]: { values: { id: number, ... } },
} ???

interface Data<T extends DataType = any(?)> extends DataGeneric<T>{
 type: DataType,
 // and all the other fields are generic from T = DataType
}
const someData: Data[] | undefined = fetchData();

// suggesting that the `type` of element[1] is "t2"
// and VSCode suggestions works correctly too,
// consider that there is an object-field `values` with `id` key
someData[1].values.id 

Thanks in advance!提前致谢!

If you're checking an object's type property against a string or numberliteral to discriminate what type of data the rest of the object is holding, then you probably want to use a discriminated union instead of generics.如果您要根据字符串或数字文字检查对象的type属性以区分 object 的 rest 所持有的数据类型,那么您可能想要使用可区分的联合而不是 Z56B97998B338B977DFF05A2888EEE。 That's basically a union type where the members of the union have a common discriminant key.这基本上是一种联合类型,其中联合的成员有一个共同的判别键。 In your case it could look like this:在您的情况下,它可能如下所示:

enum DataType {
  T1 = "t1",
  T2 = "t2"
}

type Data =
  { type: DataType.T1, content: number } |
  { type: DataType.T2, values: { id: number } }

Then when you have a value data of type Data , you can check its type property and the compiler knows that it should narrow data to the right member of the union:然后,当您有一个Data类型的值data时,您可以检查它的type属性,编译器知道它应该将data缩小到联合的正确成员:

declare const someData: Data[] | undefined;

if (someData) {
  someData.forEach(data => {
    if (data.type === DataType.T1) {
      data.content.toFixed();
    } else {
      data.values.id.toFixed();
    }
  })
}

Playground link to code Playground 代码链接

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

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