在Typescript中,有没有一种方法可以使字符串数组像一个;还是拆分类型;?

在Typescript中,有没有一种方法可以使字符串数组像一个;还是拆分类型;?,typescript,types,Typescript,Types,我指的是字符串|数字|布尔值。如果你有更好的名字,我会更新这个问题 我正在开发一个包含标签列表的界面,类似于: type Tags = "big" | "small" | "sharp" | "dull"; interface Shape { name: string; tags: Tags[]; } 在代码的后面,我想列出所有可能的标记。在我的实际代码中,标记的数量远远大于四个,因此我想使用类型断言来确保列出所有标记。我该怎么做 我可以想象的两种方式是: 由于明显的原因(嵌套条件),

我指的是
字符串|数字|布尔值
。如果你有更好的名字,我会更新这个问题

我正在开发一个包含标签列表的界面,类似于:

type Tags = "big" | "small" | "sharp" | "dull";
interface Shape {
  name: string;
  tags: Tags[];
}
在代码的后面,我想列出所有可能的标记。在我的实际代码中,标记的数量远远大于四个,因此我想使用类型断言来确保列出所有标记。我该怎么做

我可以想象的两种方式是:

  • 由于明显的原因(嵌套条件),无法从“或拆分类型”创建数组
  • 基于字符串数组断言类型。这似乎是很有可能的,我知道
    document.createElement(“a”|“div”|“span”…)
    对它有神奇的作用,尽管我找不到合适的关键字来确定它的名称
  • 我的理想是这样的:

    // Not real code
    const Tags = ["big", "small", "sharp", "dull"];
    interface Shape {
      name: string;
      tags: Array<item in Tags>;
    }
    
    //不是真正的代码
    常量标记=[“大”、“小”、“尖”、“暗”];
    界面形状{
    名称:字符串;
    标签:数组;
    }
    
    那么,有没有办法让字符串数组像一个或拆分类型一样工作?

    在我的实际代码中,标记的数量远远大于四个,因此我想使用类型断言来确保列出所有标记。我该怎么做

    我最近不得不这么做,我们有两个选择

  • 属性作为对象属性的对象
  • 一个数组和一个接口,因此每当您向标记中添加新项时,您都会同时添加到数组和对象中
  • 这似乎是个人喜好的问题

    我会使用一个对象
    标记
    来强制执行您想要的类型安全

    class Tags {
        public one: string;
        public two: string;
        public three: string;
        constructor({ one, two, three }: Tags) {
            this.one = one;
            this.two = two;
            this.three = three;
        }
    }
    
    interface Shape {
        name: string;
        tags: Tags;
    }
    
    let a = new Tags({}); // <-- This will fail because is not type Tag
    a = new Tags({ one: '1' }); // <-- This will fail because it hasn't type two or type three
    a = new Tags({ one: '1', two: '2', three: '3' }); // <-- This will pass
    
    无论我们在何处使用它们或传递标记,我们都将其作为对象传递:

    interface Shape {
      name: string;
      tags: Tags;
    }
    

    一切都是
    的用法取决于您的应用程序
    ,对于我们来说,我们需要将数组传递给一些服务,并在以数组值作为属性的对象上键入检查,因此我们选择了2,因为这阻止了通过
    Object.Keys(标记)将数组转换为键
    使用
    Shape

    对服务返回的内容进行类型检查。我最终使用的解决方案是创建一个文件,用于容纳包含两个导出的所有标记:

  • 标记
    类型,它是标记可以包含的每个字符串的联合类型
  • tagList
    数组,类型为
    {[key-in-Tags]:null}
    的对象的属性列表。此对象类型要求每个标记都表示为一个道具(仅此而已)


  • 为什么不使用枚举?@sephered他们被称为。可能的重复(答案是将数组转换为联合,这似乎是您的问题)这是一个可能的解决方案,但不是唯一的解决方案,甚至可能不是一个合适的解决方案。
    interface Shape {
      name: string;
      tags: Tags;
    }
    
    export type Tags = "featured" | "design" | "fabrication" | "development" | "audio" 
    | "imagery" | "social" | "leadership" | "writing" | "3d" | "interactive" | "work";
    
    // the following typedef will assert that every tag is added as a prop
    const tagsObject: {[key in Tags]: null} = {
        featured: null,
        design: null,
        fabrication: null,
        development: null,
        audio: null,
        imagery: null,
        social: null,
        leadership: null,
        writing: null,
        "3d": null,
        interactive: null,
        work: null,
    }
    // because the tagsObject must have all tags as props to pass type assertion
    // tagList will always contain every tag
    const tagList: Tags[] = Array.from(Object.keys(tagsObject)) as any;
    export { tagList };