我想要声明一个类型的数组,该数组最多只能包含以下字符串之一:'first'
、'second'
、'third'
。
这个数组的一些有效示例:
[]
[ 'first' ]
[ 'first', 'second' ]
[ 'first', 'second', 'third' ]
[ 'first', 'third' ]
[ 'second', 'third' ]
一些无效的数组:
[ 'other-value' ]
// 不允许其他值[ 'first', 'first' ]
// 不允许重复
我写的类型:
export enum MyOptions {
first = 'first',
second = 'second',
third = 'third'
}
export type MyType = {
name: string;
email: string;
listings: {
MyOptions;
}[];
};
它有一个警告,指出成员'MyOptions'隐式具有'any'类型,但更好的类型可以从使用中推断出来。
所以如果将其更改为:
export type MyType = {
name: string;
email: string;
listings: {
options: MyOptions;
}[];
};
现在没有警告,但它有那个额外的options
值,我认为不必添加。
有什么方法可以解决这个问题吗?
[ 'first', 'first' ]
),这是不应该的。我在问题描述中已将其添加为无效数组,也许之前没有表述清楚。 - Leo Messi