Typescript和Angular2-highcharts:属性'系列'在类型'对象'上不存在
问题描述:
当我尝试在Angular中实现以下plunkr时,我收到以下错误消息。Typescript和Angular2-highcharts:属性'系列'在类型'对象'上不存在
Property 'series' does not exist on type 'Object'.
http://plnkr.co/edit/OQSFSKisIIWAH0megy4d?p=preview
我已经安装了 “angular2-highcharts”: “^ 0.5.5”, 和打字 “@类型/ highcharts”: “^ 5.0.5”,
任何帮助,将不胜感激。
答
当您将其键入Object
时,编译器不知道该属性series
是否存在于this.options
中。
为了克服这一点,你可以删除打字的财产(懒惰的出路):
class AppComponent {
options: any;
}
或者你可以让编译器通过直接分配给它这样this.options
将推断物体的类型输入正确:
class AppComponent {
options = {
chart: {
zoomType: 'xy'
},
series: ...
// ...
};
}
或在接口定义options
类型:
interface Options {
series: any[], // Should be typed to the shape of a series instead of `any`
// and type other props like "chart", "title"
}
class AppComponent {
options: Options;
constructor() {
this.options = {
chart: {
zoomType: 'xy'
},
series: ...
// ...
};
}
}