简体   繁体   中英

How to loop an object in Typescript? Element implicitly has an 'any' type because index expression is not of type 'number'

Getting

Element implicitly has an 'any' type because index expression is not of type 'number' .

interface User {
    name: string;
    username: string;
    profileImage: string;
}

let user:User = {
    name: 'john',
    profileImage: './1.jpg',
    username: 'john',
}

for(let userData of Object.keys(user)){
    console.log(userData[userData])
}

You can try

interface User {
    name: string;
    username: string;
    profileImage: string;
}

let user:User = {
    name: 'john',
    profileImage: './1.jpg',
    username: 'john',
}

for(let key in user){
    console.log(user[key as keyof User])
}

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