How to let typescript know about err type?
type InputProps = {
err?: boolean
}
export const Input = forwardRef<HTMLInputElement, React.ComponentPropsWithoutRef<'input'>>(({ err, ...rest }, ref) => {
// some use for err here
return <StyledInput {...rest} ref={ref} />
})
const StyledInput = styled.input<InputProps>`
box-shadow: inset 0 0 0 1px ${({ err, theme }) => (err ? theme.badColor : theme.primaryColor)};
`
Error is:
Property 'err' does not exist on type 'PropsWithChildren, HTMLInputElement>, "form" | "style" | "title" | "pattern" | "key" | "accept" | "alt" | "autoComplete" | ... 276 more ... | "onTransitionEndCapture">>'.ts(2339)
Oh, found the answer myself, you just need to add & InputProps:
type InputProps = {
err?: boolean
}
export const Input = forwardRef<HTMLInputElement, React.ComponentPropsWithoutRef<'input'> & InputProps>(({ err, ...rest }, ref) => {
// some use for err here
return <StyledInput {...rest} ref={ref} />
})
const StyledInput = styled.input<InputProps>`
box-shadow: inset 0 0 0 1px ${({ err, theme }) => (err ? theme.badColor : theme.primaryColor)};
`
Use the InputProps
-type as your second generic argument:
export const Input = forwardRef<HTMLInputElement, InputProps>(({ err, ...rest }, ref) => {
return <StyledInput {...rest} ref={ref} />
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.