我正在为第三方js库创建一个TypeScript定义文件。其中一个方法允许使用options对象,而options对象的一个属性接受列表中的字符串:"collapse"、"expand"、"end-expand"和"none"。

我有一个接口的选项对象:

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style?: // "collapse" | "expand" | "end-expand" | "none"
}

接口是否可以强制执行这一点,因此如果您包含带有brace_style属性的IOptions对象,它将只允许可接受列表中的字符串?


当前回答

TS提供了对特定字符串值的类型化,这些值称为字符串文字类型。

下面是一个如何使用它们的例子:

type style =  "collapse" | "expand" | "end-expand" | "none";

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style1?:  "collapse" | "expand" | "end-expand" | "none";
  brace_style2?:  style;
}

// Ok
let obj1: IOptions = {brace_style1: 'collapse'};

// Compile time error:
// Type '"collapsessss"' is not assignable to type '"collapse" | "expand" | "end-expand" | "none" | undefined'.
let obj2: IOptions = {brace_style1: 'collapsessss'};

其他回答

试试这个

export type ReadingTypes = 'some'|'variants'|'of'|'strings';

export interface IReadings {
   param:ReadingTypes
}

编辑:非常感谢你的支持,但是随着时间的推移,我也逐渐成长为一名开发者:),现在在大多数情况下,我不会再推荐这种方法了。是的,它仍然有效,但重点是上面的结构非常类似于enum结构,所以为什么不使用enum代替(优点如下):

export enum ReadingTypes {
    Some = 'some',
    Variants = 'variants',
    Of = 'of',
    Strings = 'strings',
}
export interface IReadings {
   param: ReadingTypes
}

优点:(是的,可能它更像IMHO,我理解,但是,尽管如此)

例如,当您在代码中看到它时,它更具有可读性

if(item.reading === 'some') {
...
}
// vs 
if(item.reading === ReadingTypes.Some) {
...
}

在第一种情况下,当您阅读代码时,乍一看,.reading字段只能包含一些特定的参数,而不是任何字符串值。

当你写代码时,如果你使用枚举,你的编辑器会有更好的帮助——记住enum的名字就足够了,它会告诉你enum的所有变体。是的,用第一种类型('一些' | '变体'…)它也可以这样做,但它做得较少。嗯. .急切地

这在1.8版本中作为“字符串文字类型”发布

Typescript有什么新功能-字符串文字类型

本页示例:

interface AnimationOptions {
  deltaX: number;
  deltaY: number;
  easing: "ease-in" | "ease-out" | "ease-in-out";
}

您很可能需要枚举选项。但如果你不这样做,并且你真的需要你的const存在,这里的一些回答帖子的keyOf是有效的,只是稍微修改了一下:

export const BRACE_STYLES = {
  collapse: 'collapse',
  'end-expand': 'end-expand',
  expand: 'expand',
  none: 'none'
}

export type BraceStyle = keyof typeof BRACE_STYLES

export interface IOptions {
  indent_size?: number
  indent_char?: string
  brace_style?: BraceStyle
}

这实际上会让你得到你想要的"collapse" | "expand" | "end-expand" | "none"效果,仍然允许const存在,而不需要对类型进行硬编码。

Enum可能是最好的解决方案,但如果你的值是一个const对象的键,你不能改变它,语法将是

brace_style?: typeof BRACE_STYLES[keyof typeof BRACE_STYLES];

其中BRACE_STYLES是const对象的名称

TS提供了对特定字符串值的类型化,这些值称为字符串文字类型。

下面是一个如何使用它们的例子:

type style =  "collapse" | "expand" | "end-expand" | "none";

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style1?:  "collapse" | "expand" | "end-expand" | "none";
  brace_style2?:  style;
}

// Ok
let obj1: IOptions = {brace_style1: 'collapse'};

// Compile time error:
// Type '"collapsessss"' is not assignable to type '"collapse" | "expand" | "end-expand" | "none" | undefined'.
let obj2: IOptions = {brace_style1: 'collapsessss'};