typescript - giá trị: UnpackNestedValue<PathValue<T, Path<T>>>
Tôi đang sử dụng react-hook-form và tôi nhận được lỗi này value: UnpackNestedValue<PathValue<T, Path<T>>> mỗi khi tôi truyền giá trị dưới dạng prop.
Đây là thành phần nút chuyển của tôi:
type Props<T> = {
height: number
} & UseControllerProps<T> &
InputHTMLAttributes<HTMLInputElement>
function SwitchButton<T>(props: Props<T>) {
const {
field: { onChange, value },
} = useController(props)
return (
<Switch height={props.height}>
<input type='checkbox' checked={value} onChange={onChange} />
<Slider height={props.height}></Slider>
</Switch>
)
}
Trong đoạn mã trên, tôi nhận được lỗi ở thuộc tính checked: Kiểu 'UnpackNestedValue<PathValue<T, Path<T>>>' không thể gán cho kiểu 'boolean | undefined'.
Đây là mẫu của tôi:
const defaultValues = {
title: '',
project: {
id: '',
title: ''
},
isOpen: false,
...
}
const { handleSubmit, control, setError, watch } =
useForm<FormType>({
defaultValues,
})
<form onSubmit={handleSubmit(onSubmitHandler)}>
<SwitchButton<FormType>
name='isOpen'
height={20}
control={control}
/>
...
</form>
import { NestedValue } from 'react-hook-form'
type FormType = {
title: string
project: NestedValue<{
id: string
title: string
}>
isOpen: boolean
...
}
Tôi cũng đang gặp lỗi giống nhau trong tất cả các thành phần biểu mẫu khác, có ai biết cách khắc phục không? Cảm ơn