React.ButtonHTMLAttributes传入的泛型

来源:4-11 精益求精 - Buton 组件编码第二部分

5942

2022-12-19

老师好,我看您传入的泛型是HTMLElement,为什么不是HTMLButtonElement,HTMLButtonElement不是更准确吗?

type NativeButtonProps = BaseButtonProps &
  React.ButtonHTMLAttributes<HTMLButtonElement>;
type AnchorButtonProps = BaseButtonProps &
  React.AnchorHTMLAttributes<HTMLAnchorElement>;

export type ButtonProps = Partial<NativeButtonProps & AnchorButtonProps>;
写回答

1回答

张轩

2022-12-19

同学你好

完全可以的,这里可以使用更精确的类型,这个类型如果你去追踪泛型的话,最终是在

DOMAttributes 上面的,然后会反射到对应的事件响应上
onFocus?: FocusEventHandler<T> | undefined;

如果想要事件获取更精细的类型,可以尝试传入更准确的类型。

0
0

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2123 学习 · 959 问题

查看课程