[英]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.