简体   繁体   English

如何在 typescript 的类型别名中嵌套类型别名数组?

[英]how to nest an array of type alias in a type alias in typescript?

type subProp = {
  id: string,
  name: string
};

type ParentProps = {
  subscriptions?: [
    {
      id: string,
      items: [???Array Of subProp???]
    }
  ]
}

Is this case doable in typescript with only type alias?这种情况在 typescript 中是否可行,只有类型别名? If so, how to do it?如果是这样,该怎么做? I can't find any viable option online.我在网上找不到任何可行的选择。 If no, what's the alternative?如果没有,有什么替代方案?

You can declare items as an array of subProps and subscriptions as an array of a type with id and items :您可以将items声明为subPropsarray ,并将subscriptions声明为具有iditems的类型的数组:

type subProp = {
  id: string,
  name: string
};

type ParentProps = {
    subscriptions?: {
        id: string,
        items: subProp[];
    }[];  
}

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

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