TypeScript:
Sending an HTTP request

How to:

In TypeScript, you’d typically use the Fetch API to send HTTP requests. Here’s a quick example, using async/await for simplicity:

async function fetchData(url: string): Promise<void> {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error('Fetch error:', error);
  }
}

fetchData('https://jsonplaceholder.typicode.com/todos/1');

Sample output for a successful request:

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Deep Dive

HTTP requests have been paramount since the dawn of the web; they’re how browsers and servers chat. Before fetch came XMLHttpRequest (XHR), which did the job but felt like paperwork. fetch, a modern alternative, is promise-based, cleaner, and part of the window object in most modern browsers.

Alternatives to fetch in TypeScript include libraries like Axios, which provide more features and are sometimes easier to handle. Axios automatically transforms JSON data, handles request cancellation, and offers better error handling.

Behind the scenes, TypeScript compiles down to JavaScript. When you send an HTTP request using fetch, you’re essentially using the browser’s native Fetch API. TypeScript’s type-checking augments your code stability by catching type errors at compile-time.

See Also