简体   繁体   中英

Typescript - property does not exist on type '{..}'

I have a method which returns the following object: 在此处输入图片说明

Later on, when I wish to get a property of this object, eg name I get the following issue: 在此处输入图片说明

What am I doing wrong here and how can I avoid this?

It is an array of objects. either you need a object or you will have to find the index within that array. like conversationMetaData[index].name

conversationMetaData is an array. So it's obvious it doesn't have name attribute. You have to access the corresponding element in the array like below.

conversationMetaData[id].name;

TS doesnt know what type of object you are intending to return unless you provide a type . You can use interfaces for this purpose

eg:

createObject: SomeType = () => {
// Your function returning object of 'SomeType'
}

export interface Sometype {
    name: string,
    age: number
// etc..
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM