Typescript/러닝 타입스크립트 연습문제

(3장) Primitive Cooking - 2. Recipes

띵킹 2023. 4. 6. 21:08

https://github.com/LearningTypeScript/projects/tree/main//projects/unions-and-literals/primitive-cooking/02-recipes

 

GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledge of TypeScript.

Hands-on real world projects that will help you exercise your knowledge of TypeScript. - GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledg...

github.com

샐러드 재료들이 맛있어 보여요, 감사합니다!
다음은 내가 가장 좋아하는 요리법 목록입니다.

이것들은 TypeScript 컴파일러를 있는 그대로 잘 통과하는 것 같습니다. 
그러나 문제가 있습니다. 향후 레시피가 동일한 난이도 및 그룹 타입을 유지하는지 확인하고 싶습니다.
리터럴 타입의 유니온을 사용해주시겠습니까? 둘 다 세 가지 가능한 값만 가져야 합니다.

이번에는 런타임 코드가 대부분 잘 작동합니다. 
그러나 그룹 값 중 하나에 오타가 있습니다. 문제를 해결해야 합니다.
그렇지 않으면 타입 어노테이션을 수정해야 할 것입니다. 

#파일
index.ts: 여기에서 타입 어노테이션 수정
solution.ts: 솔루션 코드
// Please clarify any overly wide (permissive) type annotations here! ✨
let difficulty: 1 | 2 | 3;
let group: "appetizer" | "entree" | "dessert";
let title: string; 

// Start with something quick and painless to prepare...
difficulty = 1;
group = "appetizer";
title = "Raspberry Vinaigrette Salad";
console.log(`[${group}] ${title}: ${difficulty}/3 difficulty`);

// Next up, a nice hearty dish to center the meal...
difficulty = 2;
group = "entree";
title = "Cauliflower Steaks";
console.log(`[${group}] ${title}: ${difficulty}/3 difficulty`);

// Make a real impact with fancy delectable desserts...
difficulty = 3;
group = "dessert";
title = "Coconut Chocolate Ganache";
console.log(`[${group}] ${title}: ${difficulty}/3 difficulty`);

// Send everyone off with a nice closer.
difficulty = 1;
group = "dessert";
title = "Biscuits and Coffee";
console.log(`[${group}] ${title}: ${difficulty}/3 difficulty`);

export {};

원시 타입을 리터럴 타입으로 고치는 문제. 

의도된 오타를 집어넣어서 리터럴 타입의 유용성을 학습하게 하려는 의도가 친절하게 느껴졌다.

728x90