场景:
给div元素添加自定义属性,作为埋点点击位标识,遇到ts报错
错误提示:
不能将类型“{ children: string | undefined; className: any; clstag: string; }”分配给类型“DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>”。
类型“DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>”上不存在属性“clstag”。ts(2322)
错误截图:
解决办法:
typings.d.ts 文件中添加以下接口声明即可
// typings.d.ts
declare namespace React {
interface HTMLAttributes<T> extends HTMLAttributes<T> {
clstag?: string
}
interface RefAttributes<T> extends RefAttributes<T> {
clstag?: string
}
}