Praveen
Praveen

Reputation: 1802

Argument of type 'string' is not assignable to parameter of type 'never' in typeScript

I am creating a form control in react+formik type script and having the type error "Argument of type 'string' is not assignable to parameter of type 'never'."

This is the type I am using:

export type FormikField = {
  field:{
    name: string;
  onBlur: () => void;
  onChange: () => void;
  value: [];
  }
};
export type FormControlProps = {
  name: string;
  label: string;
  options?: { key: string | number; value: string}[];
};

const Checkbox = (props: FormControlProps) => {
    const{name, label, options, ...rest} = props;

  return (
    <div className='form-control'>
        <label>{label}</label>
        <Field name={name} {...rest}>
            {
                ({field}: FormikField)=>{
                  console.log('field', field);
                  
                return options?.map((option)=>{
                  return(
                    <Fragment key={option.key}>
                    <input {...field} type="checkbox" id={option.value} checked={field.value.includes(option?.value)} value={option.value}/>
                    <label htmlFor={option.value}>{option.key}</label>
                      </Fragment>
                  )
                })
                }
            }
        </Field>
        <ErrorMessage name={name} component={TextError} />
    </div>

{field.value.includes(option?.value)} Here is in the issue.
I added never, null, unknown with options value type, thinking it will help but nothing worked. enter image description here

Upvotes: 0

Views: 3527

Answers (1)

MGX
MGX

Reputation: 3521

Sorry I cannot comment.

Could you post your FormikField interface ?

From only this code, it would seem that FormikField.value is of type [], which indeed results in never when using array operators. Try setting it to string[] instead.

Upvotes: 1

Related Questions