简体   繁体   English

Typescript 元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型

[英]Typescript Element implicitly has an 'any' type because expression of type 'any' can't be used to index type

I have component that accept Item, ant I use interface to describe it, but inside component I use next construction (I use another property to get currentItem field)我有接受项目的组件,ant 我使用接口来描述它,但在组件内部我使用下一个构造(我使用另一个属性来获取 currentItem 字段)

modalComponentsData.reduce((acc: any, el: any) => {
         acc[el.name] = currentItem[el.name]
         return acc
       }, {}),

And I get next warning我得到下一个警告

TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ name: string; id?: string | undefined; }'

interface IItem{
  name: string,
  id: string
}

export default function ModalGroupComponent({currentItem}: IItem{

....some logic

modalComponentsData.reduce((acc: any, el: any) => {
         acc[el.name] = currentItem[el.name]
         return acc
       }, {}),
}

Try this:尝试这个:

interface IItem {
    name: string;
    id: string;
}

interface IItemDict {
    [key: string]: IItem
}

interface IComponent {
    currentItem: IItemDict;
}

interface IElement {
    name: string;
}

export default function ModalGroupComponent({currentItem}: IComponent) {

    // ....some logic

    modalComponentsData.reduce((acc: IItemDict, el: IElement) => {
        acc[el.name] = currentItem[el.name]
        return acc
    }, {});
}

暂无
暂无

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

相关问题 TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript:元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引“Assignable”类型 - TypeScript: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Assignable' 元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引 TypeScript 中的“{}”类型 - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' in TypeScript Typescript 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Typescript Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type TypeScript 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}” - TypeScript Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}' TypeScript:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}” - TypeScript - Element implicitly has an 'any' type because expression of type '“storyFile”' can't be used to index type '{}' React typescript - 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型 - React typescript - Element implicitly has an 'any' type because expression of type 'string' 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 in React Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM