簡體   English   中英

在打字稿中聲明對象數組

[英]Declare Array of Object in typescript

我想聲明:

type ArticleModel = {
    _id: number;
    name: string;
}

然后在我的課上:

public models: ArticleModel[];

模型應該在哪里,您可能會猜到一個ArticleModel數組。

但是node_modules / webpack / node_modules / webpack-core / lib / NormalModuleMixin.js拋出:

error TS2322: Type 'Object[]' is not assignable to type '{ _id: number; name: string; }[]'.
  Type 'Object' is not assignable to type '{ _id: number; name: string; }'.
    Property 'id' is missing in type 'Object'..

任何幫助,將不勝感激。

提前致謝。

好的,不好,我稍后將變量重新聲明為:

let models:Array<Object> = [];

干杯

如果要在創建類時實例化對象數組,則可以執行以下操作:

public models: ArticleModel[] = [];

這是在類構造函數中代替的。

暫無
暫無

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

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