TypeScript Tutorial in Hindi #41 - API Call in TypeScript

API call In TS

Write code for API call.

Define type for response.

Apply data type to API response.

Interview Question


apiCall.ts


interface APIType{
userId: string
id: number,
title: string
completed: boolean
}

async function apiCallHandling():Promise<APIType>{
const result = await fetch('https://jsonplaceholder.typicode.com/todos/4');
const data = await result.json();
// console.log(data);
return data
}

apiCallHandling().then((data:APIType)=>{
console.log(data);
})


apiCall.js

"use strict";
async function apiCallHandling() {
const result = await fetch('https://jsonplaceholder.typicode.com/todos/4');
const data = await result.json();
// console.log(data);
return data;
}
apiCallHandling().then((data) => {
console.log(data);
});