To use an API in TypeScript, you can make use of the popular axios
library. Here's an example of how to use it:
index.ts336 chars19 lines
In this example, we have first defined an interface for the Todo
object. We then make use of axios
to make an HTTP GET request to the API endpoint.
We await the response and then return the response's data as an array of Todo
objects. This function returns a Promise
of an array of Todo
objects.
Finally, we call the fetchTodos
function and log the returned todos to the console.
Note that in order to use axios
, you must first install it via npm or yarn:
index.ts18 chars2 lines
or
index.ts15 chars2 lines
gistlibby LogSnag