简体   繁体   English

如何在 Typescript 中循环 object? 元素隐式具有“任何”类型,因为索引表达式不是“数字”类型

[英]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' .元素隐式具有'any'类型,因为索引表达式不是'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])
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问typescript中的Enum? 给出错误“元素隐式具有任何类型,因为索引表达式不是数字类型” - How to access Enum in typescript ? giving error "Element implicitly has an any type because index expression is not of type number" 元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型 - Element implicitly has an 'any' type because index expression is not of type 'number' Object.keys 迭代导致 Typescript 错误“元素隐式具有 'any' 类型,因为索引表达式不是类型 'number'” - Object.keys iteration causing Typescript error “Element implicitly has an 'any' type because index expression is not of type 'number'” Typescript 元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引 - Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index TypeScript TS7015:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型 - TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' TS 7015:元素隐式具有“任何”类型,因为索引表达式不是 Object 的“数字”类型。getElementsByTagName 的键 - TS 7015: Element implicitly has an 'any' type because index expression is not of type 'number' for Object.keys of getElementsByTagName TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript Typescript “元素隐式具有‘any’类型,因为类型‘Rank’的表达式不能用于索引类型‘IPowerCards’” - Typescript "Element implicitly has an 'any' type because expression of type 'Rank' can't be used to index type 'IPowerCards'" TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM