typescript是否有办法从属性中删除数组?



我有这个模型:

export interface SizeAndColors {
size: string;
color: string;
}[];

然后我有另一个模型我需要sizeAndColor但是没有数组

export interface Cart {
options: SizeAndColors
}

我怎么能在选项中说我想要这个没有数组的接口?可以吗?

假设SizeAndColors确实被声明为数组类型,其中元素是具有sizecolor属性的对象(问题中的内容似乎不是这样),我建议拆分原始接口:

interface SizeAndColorsElement {
size: string;
colors: string;
}
export type SizeAndColors = SizeAndColorsElement[];

但是如果不能,您可以使用SizeAndColors[number]来访问其中的对象部分:

export interface Cart {
options: SizeAndColors[number];
}

:这是假设它确实被定义为数组类型,而问题的代码中似乎没有。

像这样定义你的接口

export interface SizeAndColors {
size: string;
color: string;
}

,只在需要时使用

export interface Cart {
options: SizeAndColors[]
}

最新更新