我是TS的新手,我想知道为什么我在以下代码中遇到类型错误(简化):
"use client";
import { ArrowDownWideNarrow, Settings } from "lucide-react";
interface LinkItemProps {
name: string;
}
const iconMap = {
Categories: <ArrowDownWideNarrow />,
Admin: <Settings />,
};
export const LinkItem = ({ name }: LinkItemProps) => {
<div>
{iconMap[name]}
</div>
}
错误在`{iconMap[名称]}中:
"Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ Categories: Element; Admin: Element; }'.
个
No index signature with a parameter of type 'string' was found on type '{ Categories: Element; Admin: Element; }'."
个
parent element
拥有以下功能:
const LINKS = [
{
href: "/admin",
name: "Admin",
id: "admin",
},
{
href: "/categories",
name: "Categories",
id: "categories",
},
];
export const Sidebar = async () => {
return (
<div>
{LINKS.map((link) => (
<div key={link.href}>
<LinkItem href={link.href} id={link.id} name={link.name} />
</div>
))}
</div>
)
}