我正在使用带有Typescript的Reactstrap并从@types/reactstrap
中键入内容。我正在尝试在HOC中使用Button
组件,因此我需要显式引用ButtonProps
类型。这是它的代码:
import React from 'react'
import {withFormState, FormState} from 'informed'
import {Button, ButtonProps} from 'reactstrap'
export const SubmitButton = withFormState<Button, {formState: FormState} & ButtonProps>(
({
formState: {pristine, invalid},
disabled = false, children,
...props
}) =>
<Button type="submit" color="success"
disabled={pristine || invalid || disabled}
{...props}>
{children}
</Button>
)
因此,基本上我只是将软件包中的ButtonProps
与其他软件包中的FormState
组合在一起,但最后props
变量应该只具有与ButtonProps
类型匹配的道具(因为formState
已从其变形)。
但是,TS指出ButtonProps
与我传递给组件的道具不匹配,即ref
属性类型不兼容。在这种情况下,我不使用裁判,因此我不太可能弄乱他们。最有可能的是,我只是不了解在这种情况下如何正确定义类型。
以下是我使用的类型的引用:
答案 0 :(得分:0)
我认为这是@types/reactstrap
类型声明中的错误。请注意,在以下更简单的测试案例中会发生相同的错误:
function test(props: ButtonProps) {
return <Button {...props}/>;
}
ButtonProps
包括React.HTMLProps<HTMLButtonElement>
,其中包括类型为ref
的{{1}}道具,但是Ref<HTMLButtonElement>
无法真正接受这样的道具,因为React将拦截{在JSX元素上指定的{1}}属性,并将其与Button
组件关联,而不是其中可能存在的任何ref
。 Button
应该更改为使用HTMLButtonElement
而不是ButtonProps
。请file an issue against DefinitelyTyped。
作为解决方法,您可以在销毁过程中删除伪造的React.AllHTMLAttributes<HTMLButtonElement>
道具:
React.HTMLProps<HTMLButtonElement>