R
R
Raey2021-01-12 17:58:21
typescript
Raey, 2021-01-12 17:58:21

What is the return type of the function that returns useCallback?

Given the function below, what type does it return? Thanks in advance.

const useSendMessage = (): ??? => {
  const dispatch = useDispatch();

  return useCallback(
    (id: number, message: Message) => {
      dispatch(Operation.sendMessage(id, message));
    },
    [dispatch]
  );
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2021-01-12
@Raey

So, just look at what useCallback returns.
Or don't write anything. TS will infer the return type itself

D
Dmitry Belyaev, 2021-01-12
@bingo347

https://github.com/DefinitelyTyped/DefinitelyTyped...
Accordingly, the type will be:
(id: number, message: Message) => void

const useSendMessage = (): (id: number, message: Message) => void => {
  const dispatch = useDispatch();

  return useCallback(
    (id: number, message: Message) => {
      dispatch(Operation.sendMessage(id, message));
    },
    [dispatch]
  );
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question