// ↓↓↓ it should look like ↓↓↓ functioncreateBlogPost (text: string, author: string, date: Date = newDate()) return{ text: text, author: author, date: date } }
// ↓↓↓ it should look like ↓↓↓ asyncfunctionloadProducts(): Promise<Product[]> { const response = await fetch("https://api.mysite.com/products"); const products: unknown = await response.json(); // any 대신에 unknown을 사용할 것 return products as Product[]; // 또한, as를 사용하여 리턴값을 반드시 정의하기 }
// 제네릭 또한 변수명처럼 사용해야한다. 말인즉슨 알아보기 쉬워야 한다 functionhead<T>(arr: T[]): T | undefined{ return arr[0]; }
// ↓↓↓ it should look like ↓↓↓ functionhead<Element>(arr: Element[]): Element | undefined{ return arr[0]; }
자바스크립트에서 제공하는 non-boolean형 타입추론 체크 대신 자발적인 타입 체크를 사용할 것
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (countOfNewMessages) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }
// ↓↓↓ it should look like ↓↓↓ functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (countOfNewMessages !== undefined) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }
Bang Bang 연산자(!!)를 사용하지 않기
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (!!countOfNewMessages) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }
// ↓↓↓ it should look like ↓↓↓ functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (countOfNewMessages !== undefined) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }
!= null은 이제 그만
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (countOfNewMessages != null) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }
// ↓↓↓ it should look like ↓↓↓ functioncreateNewMessagesResponse(countOfNewMessages?: number) { if (countOfNewMessages !== undefined) { return`You have ${countOfNewMessages} new messages`; } return"Error: Could not retrieve number of new messages"; }