我目前正在将 Next.js 项目从 JavaScript 迁移到 TypeScript,我遇到了一个错误:Property 'className' does not exist on type '{ props: ReactNode; }'
. 在 Javascript 中,我可以从道具中提取类名,但打字稿找不到类型。这是代码:
import { useRouter } from 'next/router'
import Link from 'next/link'
import { ReactNode } from 'react'
export { NavLink }
NavLink.defaultProps = {
exact: false,
}
interface NavLinkProps {
href: string
exact?: boolean
children: ReactNode
props: ReactNode
}
function NavLink({ href, exact, children, ...props }: NavLinkProps) {
const { pathname } = useRouter()
const isActive = exact ? pathname === href : pathname.startsWith(href)
if (isActive) {
props.className += ' active'
}
return (
<Link href={href}>
<a {...props}>{children}</a>
</Link>
)
}
}