Arrays 为实现接口的对象分配一个空数组,并在以后将新对象推入其中
我有以下界面:Arrays 为实现接口的对象分配一个空数组,并在以后将新对象推入其中,arrays,typescript,Arrays,Typescript,我有以下界面: export interface FacilityInformation { facilitiesInformation: [ { id: string, productType: string, productSubtype: string, nominalAmount: number, currency: string, maturity: string, fee: number, recommended
export interface FacilityInformation
{
facilitiesInformation: [
{
id: string,
productType: string,
productSubtype: string,
nominalAmount: number,
currency: string,
maturity: string,
fee: number,
recommended: number,
margin: number,
roac: number,
ep: number
}
],
allFacilities: {
amount: number,
fee: number,
roac: number,
ep: number
},
customerLevel: {
amount: number,
fee: number,
roac: number,
ep: number
},
groupLevel: {
amount: number,
fee: number,
roac: number,
ep: number
}
}
我的目标是初始化一个空对象,该对象使用空数组facilitieinformation
实现接口FacilityInformation
,然后将对象推入该数组。但是,我似乎无法初始化空数组,它要求我立即赋值:
let facilityInformation : FacilityInformation =
{
facilitiesInformation :
{ id: string, productType: string ... } [], // says string is a type but used as a value
分配空数组后,我希望执行以下操作:
facilityInformation.facilitiesInformation.push({...})
我如何首先初始化一个空数组,然后将对象推入其中?使用这种方法
let facilityInformation: FacilityInformation =
{
facilitiesInformation: [] as any,
...
} as FacilityInformation;
我收到一个错误:类型“[]”中缺少属性“0”,但类型“[{id:string,productType:string…”中需要属性“0”。我收到一个错误:属性设施信息的类型不兼容。请重试编辑的回答:请查看此stackblitz