我试图创建一个可以扩展到React组件的对象,这个对象必须包含HTML Custom Data Attribute。
import { HTMLAttributes } from 'react'
interface FooProps extends HTMLAttributes<HTMLButtonElement> {
fooProp: string
//'data-testid': string // do HTML Custom Attributes not come out of the box?
}
const fooObj: FooProps = {
fooProp: 'yay',
'data-testid': 'nay' // Object literal may only specify known properties, and ''data-testid'' does not exist in type 'Foo'.(2353)
}
const Foo = () => <button {...fooObj}></button>
字符串
在上面的例子中,我似乎不能在不显式列出自定义数据属性的情况下输入对象来允许自定义数据属性。我如何在不显式声明的情况下输入对象来允许自定义数据属性?
1条答案
按热度按时间vzgqcmou1#
虽然custom data attributes
data-*
在使用JSX语法时被TypeScript编译器识别-例如,这编译没有错误。TSPlayground
字符串
. React不provide元素属性类型别名/包含它们的接口。
为了允许在自己的类型中自定义数据属性,您可以在每个定义中显式地包含它们。
TSPlayground
型
.或者-你可以通过使用模块扩充的模式以一种更DRY的方式来实现它:
在项目中创建一个类型声明文件,其路径包含在程序的编译中(例如
src/types/react_data_attributes.d.ts
):型
参考号:microsoft/TypeScript#36812 — Add
import type "mod"
然后,在每个使用站点,将不再需要显式交集:
TSPlayground
型
关于
HTMLElement
子类型的说明:每个子类型(例如
<button>
,<a>
等)可能除了基本HTMLAttributes
中提供的属性之外还具有专门的属性,因此您需要相应地键入元素属性,以便编译器识别这些特定属性。下面是一个示例,显示了上面元素的一些专门属性:TSPlayground
型