![](/img/trans.png)
[英]How to declare Typescript type that is AnyRecord OR an object array?
[英]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.