Skip to content

How can I tell Formbit that the values are not null? #15

@TheFe91

Description

@TheFe91

I'm developing a form with formbit in Typescript and I ran into this problem:

I have my initialValues defined in this way

const schema = yup.object().shape({
  adataord: yup.string().required(),
  dadataord: yup.string().required(),
  lbage: yup.string().required(),
  td_flags: yup.string().required(),
});

type FormValues = {
  adataord: string;
  dadataord: string;
  lbage: string;
  td_flags: string;
};

const initialValues: FormValues = {
  adataord: '',
  dadataord: '',
  lbage: '-1',
  td_flags: '99',
};

const { write, form } = useFormbit({ initialValues, yup: schema });

I have a useState defined in this way

const [filters, setFilters] = useState(initialValues);

At a certain point I want to do

const handleOnFilter = () => {
  setFilters({ ...form });
};

However this raises a typescript error, saying that string | undefined is not assignable to type string, obliging me to do

const handleOnFilter = () => {
    setFilters({
      adataord: form.adataord!,
      dadataord: form.dadataord!,
      lbage: form.lbage!,
      td_flags: form.td_flags!,
    });
  };

I'm not getting why formbit addresses the types as nullables, while I defined them as non-nullables.
Am I making any mistake?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions