簡體   English   中英

初始化對象的類型不存在屬性

[英]Property does not exist on type of initialized object

我將數據分配給接口的對象。

我用optionnal字段創建了我的接口

export interface IWeek {
    type?: number;
    required?: string;
}


export interface IItem {
    code?: string;
    recipe?: IRecipes;
}

export interface IMenu {
    week?: IWeek;
    items?: IItem[];
    title?: string;
}

然后我將數據分配給對象

const data :IMenu ={} ;
data.week.type=date;
data.title=title;
recipes.map(elm => {
  data.items.push({code:code,recipe:elm})
})

我得到的錯誤是項目沒有存在於打字對象上

ERROR in src/app/validation-dialog/validation-dialog.service.ts(13,10): error TS2339: Property 'week' does not exist on type '{}'.
src/app/validation-dialog/validation-dialog.service.ts(14,10): error TS2339: Property 'title' does not exist on type '{}'.
src/app/validation-dialog/validation-dialog.service.ts(16,12): error TS2339: Property 'items' does not exist on type '{}'.

您尚未定義屬性items 首先將其初始化為空數組。

const data: IMenu = {};
data.items = [];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM