【问题标题】:Is it possible to use multiple, comma deliminated strings, as a TypeScript type? [duplicate]是否可以使用多个逗号分隔的字符串作为 TypeScript 类型? [复制]
【发布时间】:2020-08-09 09:27:37
【问题描述】:

我知道可以创建Type LayoutType = "Left" | "Right" | "Top" | "Bottom" | "VCenter",是否可以创建会导致“Left,VCenter”的类型?或者我是否正在考虑为 每个 可能的类型创建一个字符串文字?

【问题讨论】:

    标签: string typescript types literals


    【解决方案1】:

    对于这个用例,您可以添加一个类型 Type LayoutTypes = LayoutType[];,它是一个数组,可以接受您为类型 LayoutType 指定的多个值。

    【讨论】:

      【解决方案2】:

      目前在 Typescript 中是不可能的。

      你可以试试tuples

      type LayoutType = ["Left" | "Right" | "HCenter", "Top" | "Bottom" | "VCenter"];
      
      const layout: LayoutType = ["Left", "VCenter"]
      

      查看Typescript Playground

      【讨论】:

        猜你喜欢
        • 2011-03-07
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 2013-10-07
        • 1970-01-01
        • 2014-06-13
        • 2020-12-04
        • 1970-01-01
        相关资源
        最近更新 更多