简体   繁体   English

如何指定列表项的类型。 TypeScript 错误 (TS7053)

[英]How to specify a type for list items. TypeScript error (TS7053)

There is this code, with a TS error(TS7053) here- ${Titles[item]} I know it's a simple error.有这段代码,这里有一个 TS 错误(TS7053)- ${Titles[item]} 我知道这是一个简单的错误。 I'm new to TS, I don't understand how I need to specify a type for ${Titles[details]} to make this code work我是 TS 的新手,我不明白如何为 ${Titles[details]} 指定一个类型才能使此代码正常工作

import { Details } from './details';
import { Titles } from './titles';

type Keys = keyof typeof Titles;

interface Props {
  details: Details;
} 

export function TailsList({ details }: Props) {
  const keysList: string[] = Object.keys(Titles) as Keys[];
  return (
    <ul>
      {keysList?.map((item) => (
        <li key={item}>{`${Titles[item]}`}</li>
      ))}
    </ul>
)

Remove the string[] from keysList .keysList中删除string[]

keyList was typed as string[] instead of Keys[] . keyList被输入为string[]而不是Keys[]

Put it as:把它写成:

 const keysList: Keys[] = Object.keys(Titles) as Keys[];

暂无
暂无

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

相关问题 TypeScript 和 Vue class - TS7053 索引签名 - TypeScript and Vue class - TS7053 index signatures TS7053:元素隐式具有“任何”类型 - TS7053: Element implicitly has an 'any' type 错误 TS7053:元素隐式具有“任何”类型,因为“数据”类型的表达式不能用于索引类型“{}” - error TS7053: Element implicitly has an 'any' type because expression of type '“data”' can't be used to index type '{}' typescript 和 reactjs:如何使用 map - 错误 ts(7053) - typescript and reactjs : how to use map - ERROR ts(7053) TypeScript 错误 7053 - 创建一个使用 prop 覆盖样式的自定义 React 组件,我如何正确告诉 TS 类型是什么? - TypeScript Error 7053 - Creating a custom React component that overrides styles with a prop, how do I properly tell TS what the type is? TS7053:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ A:数字; B:数字 - TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ A: number; B: number "我创建的对象数组出现 ts 7053 错误(Typescript)" - Getting ts 7053 error (Typescript) with my Array of Objects that I created TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index Angular 单元测试本地存储,错误 ts(7053) - Angular unit testing the localstorage, Error ts(7053) 如何不在 a.ts 文件中指定文件类型 as.js? - How to not specify file type as .js in a .ts file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM