简体   繁体   English

打字稿错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型

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

I'm new to typescript and it's a hell trying to make some code works, can someone help me solve this ts error.我是打字稿的新手,试图让一些代码正常工作简直是地狱,有人可以帮我解决这个 ts 错误。

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ 'sections.hero': ({ sectionData, key }: props) => Element;元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{”sections.hero“:({sectionData,key}:props)=>元素; }'. }'。 No index signature with a parameter of type 'string' was found on type '{ 'sections.hero': ({ sectionData, key }: props) => Element;在类型'{'sections.hero': ({ sectionData, key }: props) => Element; }'. }'。

my code :我的代码:

type sectionDataProps = {
  sectionData: {
    __component: string;
  };
};

// Map Strapi sections to section components
const sectionComponents = {
  'sections.hero': Hero,
};

// Display a section individually
const Section = ({ sectionData }: sectionDataProps) => {
  // Prepare the component
  const SectionComponent = sectionComponents[sectionData.__component];

  if (!SectionComponent) {
    return null;
  }

  // Display the section
  return <SectionComponent data={sectionData} />;
};

// Display the list of sections
const Sections = (props: { sections: [] }) => {
  return (
    <>
      {/* Show the actual sections */}
      {props.sections.map((section) => (
        <Section
          sectionData={section}
          key={`${section.__component}${section.id}`}
        />
      ))}
    </>
  );
};

export default Sections;

In typescript you are not allowed to access properties that may not exist.在打字稿中,您不允许访问可能不存在的属性。 That means that when have a type like { a: number } , that you may only access the a property.这意味着当有像{ a: number }这样的类型时,您只能访问a属性。 Checking the b property would be a type error because it does not exist.检查b属性将是类型错误,因为它不存在。


So you have this type for the key:所以你有这种类型的键:

__component: string;

And this type for the object that key is expected to be on:这种类型的对象的键应该是:

const sectionComponents = {
  'sections.hero': Hero,
};

Which means that only one single string is allowed for this operation:这意味着此操作只允许使用一个字符串:

sectionComponents[sectionData.__component]

And that string is 'sections.hero' any other string will not be allowed.并且该字符串是'sections.hero' ,不允许使用任何其他字符串。

You can fix this a few ways.您可以通过几种方式解决此问题。


First, you could change the type of the __component property to be:首先,您可以将__component属性的类型更改为:

type sectionDataProps = {
  sectionData: {
    __component: keyof typeof sectionComponents;
  };
};

Now any string is not a valid value.现在任何string都不是有效值。 The strings must be keys of the sectionComponents object.字符串必须sectionComponents对象的键。

And the rest of you code works without modification.其余的代码无需修改即可工作。 See Playground 见游乐场


If you didn't want to change the props types, you have to change the logic of your function to make sure that you do not access a property that may not exist.如果您不想更改道具类型,则必须更改函数的逻辑以确保您不会访问可能不存在的属性。

// Display a section individually
const Section = ({ sectionData }: sectionDataProps) => {
  // Prepare the component
  if (sectionData.__component in sectionComponents) {
      const key = sectionData.__component as keyof typeof sectionComponents
      const SectionComponent = sectionComponents[key];
      return <SectionComponent data={sectionData} />;
  }

  return null
};

Here we use an in check to see if a property exists on that object.在这里,我们使用in检查来查看该对象上是否存在属性。 Then it should be be safe to manually cast that to keyof typeof sectionComponents to let typescript know that we are certifying this operation as safe.然后手动将其转换为keyof typeof sectionComponents应该是安全的,让 typescript 知道我们正在证明这个操作是安全的。

See playground 看游乐场

暂无
暂无

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

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