我正在努力让Typescript中的类型判断为使用forwardRef并具有...rest属性.
例子:
import React from 'react'
interface InputWithLabelProps {
label: string,
[rest: string]: unknown,
}
const InputWithLabel = React.forwardRef<HTMLInputElement, InputWithLabelProps>(
({ label, ...rest }, ref) => (
<>
<label htmlFor="field">{label}</label>
<input id="field" ref={ref} {...rest} />
</>
),
)
export default InputWithLabel
这将为组件提供以下签名:
(alias) const InputWithLabel: React.ForwardRefExoticComponent<Pick<InputWithLabelProps, keyof InputWithLabelProps> & React.RefAttributes<HTMLInputElement>>
import InputWithLabel
这将不会提供正确的类型判断,例如,不需要提供所需的标签属性.如果我删除forwardRef或...rest属性它可以工作,但这不是一个选项.
如何在具有以下内容的组件上使用forwardRef...rest属性并同时进行类型判断?