如何在另一个可选属性中指定可选属性



我想在会议界面的另一个可选属性位置中指定可选属性postalCode,但当我尝试这样做时,我遇到了一个错误:

export interface Conference {
name: string;
subject: string;
description: string;
lecturer: string;
type: string;
linkToMeeting: string;
place?: Place;
date: string;
time: string;
participantsLimited: string;
}
export interface Place {
address: string;
city: string;
postalCode?: string;
country: string;
}

我收到的错误:

Type 'Conference' is not assignable to type '{ name: string; subject: string; description: string; lecturer: string; type: string; place: { address: string; city: string; country: string; postalCode?: undefined; }; linkToMeeting: string; date: string; time: string; participantsLimited: string; }'.
Types of property 'place' are incompatible.
Type 'Place | undefined' is not assignable to type '{ address: string; city: string; country: string; postalCode?: undefined; }'.
Type 'undefined' is not assignable to type '{ address: string; city: string; country: string; postalCode?: undefined; }'.

我可以使用一种变通方法,例如将postalCode属性指定为";邮政编码?:任何";,但必须有一个选项将其指定为";字符串";和可选属性。

好吧,这是我犯的一个愚蠢的错误。我指定的接口没有问题。他们还好。我试图将新会议推送到一些没有指定类型conference[]的数组时遇到了问题。感谢大家的评论,并让我放心接口规范很好。

最新更新