简体   繁体   English

TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型

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

I am getting this error:我收到此错误:

  Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ foo: string; bar: string; }'.ts(7053)

In this code:在这段代码中:

const CATEGORY_COLORS = {
  foo: '#6f79F6',
  bar: '#4fA0E9',
};

const CATEGORY_LABELS = {
  foo: 'FOO',
  bar: 'BAR',
};

const ItemRenderer = ({ item }: ItemRendererPropsType): React.ReactElement => {
  return (
    <div>
      <Tag color={CATEGORY_COLORS[item.category]}>
        {CATEGORY_LABELS[item.category]}
      </Tag>
    </div>
  );
};

The error is when I hover over either CATEGORY_COLORS[item.category] or CATEGORY_LABELS[item.category] with TypeScript.错误是当我使用 TypeScript 在CATEGORY_COLORS[item.category]CATEGORY_LABELS[item.category]上进行 hover 时。 How do I resolve?我该如何解决?

When a raw object is defined in Typescript (eg CATEGORY_LABELS) - it won't allow for indexed access (eg CATEGORY_LABELS[key]) where key is a string.当在 Typescript(例如 CATEGORY_LABELS)中定义原始 object 时 - 它不允许索引访问(例如 CATEGORY_LABELS[key]),其中 key 是字符串。 In your example, I assume that item.category is of type string .在您的示例中,我假设item.category的类型为string

Either item.category should be of type keyof typeof CATEGORY_LABELS or you need to redefine CATEGORY_LABELS to allow for indexing by a random string, but that is less typesafe, since you aren't guaranteeing that you will pass in a valid key. item.category应该是keyof typeof CATEGORY_LABELS ,或者您需要重新定义 CATEGORY_LABELS 以允许按随机字符串进行索引,但这不太安全,因为您不能保证您将传递有效的密钥。

const CATEGORY_LABELS: Record<string, string> = {
  foo: 'FOO',
  bar: 'BAR',
};

暂无
暂无

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

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