reactjs – Typescript. Unable to assign variable type

Question:

  const createObligation = useCallback(
    data => {
      setIsShowNewObligation(false);
      dispatch(createObligationThunk(data));
    },
    [dispatch]
  );

  const updateObligation = useCallback(
    (data): void => {
      setInEdit(false);
      dispatch(updateObligationThunk(data));
    },
    [dispatch]
  );

        const onSubmit: any = item._isNew ? createObligation : updateObligation;
        const onCancel: any = item._isNew ? () => setIsShowNewObligation(false) : () => setInEdit(false);

I need to write a type for two constants. If I write const onSubmit: void = ... , then an error appears:

TS2322: Type '(data: any) => void' is not assignable to type 'void'.

What am I doing wrong?

Answer:

The error specified a type that has an assignable value

(data: any) => void

this is exactly what should have been specified for the variable:

const onSubmit:  (data: any) => void = ... 

Now there is an attempt to declare a variable with type void .

Scroll to Top