- 왜 타입스크랩트를 써야하나.
- TypeScipt 기초
- 함수 타입을 정의하자
- interface를 활용하자
- 타입 별칭이란?
- 연산자를 이용한 타입 정의 - 유니온 타입
- enum
- class
- generic
- promise Types
- todolist 만들어보기
- 디버깅
목차
Promise Types
// 비동기 코드,
function asyncFetchItems() {
let items = ["a", "b", "c"];
return new Promise(function (resolve) {
resolve(items);
});
}

- promise를 반환하긴 할건데 타입을 잘 모르겠어..
- 제네릭을 타입을 집어넣고 돌려받는다
fetchContacts().then((response) => {
this.contacts = response;
});
function fetchContacts(): Promise<Contact[]> {
const contacts: Contact[] = [...]
return new Promise((resolve) => {
setTimeout(() => resolve(contacts), 2000);
});
}
fetchContacts().then((response) => {
this.contacts = response;
});
- Promise의 반환값이 제너릭이기 때문에 .then메서드의 출력 결과로 나온 response 역시도 타입이 Contact[] 인 것을 알 수 있다.
🔗 Reference