원본 : https://www.delftstack.com/howto/typescript/typescript-fetch/
// Todo type interface
interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}
function fetchToDo<T>(resourceUrl: string): Promise<T> {
return fetch(resourceUrl).then(response => {
// fetching the reponse body data
return response.json<T>()
})
}
// Consuming the fetchToDo to retrieve a Todo
fetchToDo<Todo>("https://jsonplaceholder.typicode.com/todos/2")
.then((toDoItem) => {
// assigning the response data `toDoItem` directly to `myNewToDo` variable which is
// of Todo type
let myNewToDo:Todo = toDoItem;
// It is possible to access Todo object attributes easily
console.log('\n id: '+ myNewToDo.id + '\n title: ' + myNewToDo.title + '\n completed: ' + myNewToDo.completed + '\n User Id: ' + myNewToDo.userId);
});
'web > javascript' 카테고리의 다른 글
화면 스크롤 로드 체크 - new IntersectionObserver (0) | 2023.05.30 |
---|---|
Axios in Typescript (0) | 2022.09.04 |
화면 활성화 비활성화 active deactive , visibilitychange (0) | 2022.08.19 |
antd useRef current.focus (0) | 2022.06.14 |
typescript onchange moment (0) | 2022.06.12 |
댓글