Property 'xxx' does not exist on type '{}'

故事扮演 提交于 2019-12-24 07:52:07

问题


I run into a strange behavior in my angular app today.

ERROR in src/main(A,B): error TS2339: Property 'XXX' does not exist on type '{}'.

The error occured at the following line of code

xxx.subscribe(data => {
    this.var = data.XXX
});

So basically i subscribe to an observable and map the data from the observable to an global var. My angular App works as intended, so this error seems to have no effect on the app itself, but I still can't figure out whats wrong here. Below is the console log of data:

console.log(data)
//{"XXX": "test"}

回答1:


Typescript complains that you do not have the property inside the data, you can either use any or create an inteface with that property,

xxx.subscribe((data:any) => {



回答2:


You have instantiated data as just an object example : data = {}. As object doesn't have XXX property it gives this error. So solution will be data: {XXX: string, YYY: number ....} = {}

Else you can access XXX with data['XXX'] which is the easiest option



来源:https://stackoverflow.com/questions/51377151/property-xxx-does-not-exist-on-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!