0

In the code below I typed the dispatch function as "any". I am doing this to clear the error I get if I don't type it.

What is the correct way to type it with Typescript ?

export function doThing() {
  console.log("works")
  return (dispatch:any) => {  // any ?       
    fetch('https://whatever.com/some-api').then((response) => {
      return response.json()
    }).then((data) => {

      dispatch(pingApiAction(data))
    })
  };
}
William
  • 4,422
  • 17
  • 55
  • 108

1 Answers1

0

You should specify an interface for the return from pingActionResult and then use that in the definition of dispatch

Given, for example:

interface PingActionResult
{
    field1: string;
    field2: string;
}

Your code might look like:

export function doThing() {
  console.log("works")
  return (dispatch:(result:PingActionResult) => void) => {  // any ?       
    fetch('https://whatever.com/some-api').then((response) => {
      return response.json()
    }).then((data) => {

      dispatch(pingApiAction(data))
    })
  };
}

// just for illustration
function pingApiAction(data:{}): PingActionResult{
    return {field1: "foo", field2: "bar"}
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193