How to make an HTTP request in TypeScript?
Hello everyone! Please advise how to properly make an HTTP request in TypeScript. I'm writing a simple application to fetch data from a public REST API (e.g., JSONPlaceholder). The problem is that I don't fully understand how to type the response from the server and which method is best to use in a modern TypeScript project.
I tried using the built-in fetch, but I ran into the issue that the response comes as any, and TypeScript complains about missing types. Here's my current code:
async function fetchData() {\n const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');\n const data = await response.json();\n console.log(data);\n}\nI expect that data will have a specific structure, for example: { userId: number, id: number, title: strin
No answers yet.
Log in to leave an answer
Blogs