简体   繁体   English

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

[英]React typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

I have a demo here这里有一个演示

I know this isn't a good question but the working code in my demo is causing an error in my actual code.我知道这不是一个好问题,但我演示中的工作代码导致我的实际代码出错。

I have a simple React typescript app that just displays value from json data.我有一个简单的 React typescript 应用程序,它只显示来自 json 数据的值。

In my demo this works but in my actual code (that is nearly exactly the same) I get a typescript error.在我的演示中,这有效,但在我的实际代码中(几乎完全相同),我收到了一个打字稿错误。

The error is here {ProductData[key]错误在这里{ProductData[key]

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型

I think I need to create an interface to describe the json and link that but I'm not sure how that interface should look我想我需要创建一个接口来描述 json 并链接它,但我不确定该接口应该是什么样子

<div>
  <ul>
    {Object.keys(ProductData).map(key => (
      <li key={key}>{ProductData[key].ProductSize.min}</li>
    ))}
  </ul>
</div>

can try this way, but not recommend to iterate object to create array as Object.keys (or any iteration over object) does not guarantees order of items.可以尝试这种方式,但不建议迭代对象来创建数组,因为Object.keys (或任何对对象的迭代)不能保证项目的顺序。

just use array for this usecase.只需将数组用于此用例。

also, type infer doesnot always work like magic.此外,类型推断并不总是像魔术一样工作。 there are lots of written posts which describes how and why it works that way.有很多书面文章描述了它如何以及为什么这样工作。 ex) https://stackoverflow.com/a/55012175/5755608 ex) https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208例如) https://stackoverflow.com/a/55012175/5755608前) https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208

// example 1
import ProductData from "./product.json";

type ProductDetail = {
  ProductSize?: {
    min: number;
    max?: number;
  };
  ProductHeight?: {
    min: number;
    max?: number;
  };
  ProductSpacing?: number;
  ProductWeight: number;
};

const App = () => {
  return (
    <div>
      <ul>
        {Object.entries(ProductData).map(
          ([key, value]: [string, ProductDetail]) => (
            <li key={key}>{value.ProductSize.min}</li>
          )
        )}
      </ul>
    </div>
  );
};

also can try this way, but has few limitations.也可以尝试这种方式,但几乎没有限制。

  1. can not infer type of ProductData as you might imagine due to limitation of infos由于信息的限制,无法像您想象的那样推断 ProductData 的类型
  2. need to cast type of key because it could also be type symbol需要转换键的类型,因为它也可能是类型符号
// example 2
const App = () => {
  return (
    <div>
      <ul>
        {Reflect.ownKeys(ProductData).map((key) => (
          <li key={key as string}>{ProductData[key].ProductSize.min}</li>
        ))}
      </ul>
    </div>
  );
};

暂无
暂无

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

相关问题 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 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 带有 React &gt; Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - 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 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 反应 typescript 错误 - 元素隐式具有任何类型,因为类型字符串的表达式不能用于索引类型 {} - react typescript error - element implicitly has an any type because expression of type string cant 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 '{}' 元素隐式具有 &#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