代码之家  ›  专栏  ›  技术社区  ›  ey dee ey em

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

  •  0
  • ey dee ey em  · 技术社区  · 4 年前
    type subProp = {
      id: string,
      name: string
    };
    
    type ParentProps = {
      subscriptions?: [
        {
          id: string,
          items: [???Array Of subProp???]
        }
      ]
    }
    

    这种情况在只有类型别名的打字脚本中可行吗?如果是这样,该怎么做?我在网上找不到任何可行的选择。如果没有,还有什么选择?

    1 回复  |  直到 4 年前
        1
  •  2
  •   eol    4 年前

    你可以申报 items 作为a array 属于 subProps subscriptions 作为具有以下类型的数组 id 项目 :

    type subProp = {
      id: string,
      name: string
    };
    
    type ParentProps = {
        subscriptions?: {
            id: string,
            items: subProp[];
        }[];  
    }
    
    推荐文章