[Leaning Typescript 🦜] Primitive Cooking

πŸ₯— Primitive Cooking

Those pesky Java programmers are at it again!
I wrote a few functions for a friend to help arrange meals when I have guests over. The friend worked with their enterprise application development team to β€œimprove” the code. Next thing I know, they’ve replaced all my TypeScript-y literal and union types with plain old primitive types.
Those Java fans are perfectly good developers -and lovely people- but we don’t see eye-to-eye on type systems. Now TypeScript is reporting type errors on my code. Could you please correct the type annotations in my files – and maybe a couple bugs the improved types helped TypeScript find?

μ„±κ°€μ‹  μžλ°” ν”„λ‘œκ·Έλž˜λ¨Έλ“€μ΄ 또 μ‹œμž‘μ΄μ•Ό!
λ‚˜λŠ” 친ꡬλ₯Ό μœ„ν•΄ μ†λ‹˜λ“€μ΄ 왔을 λ•Œ 식사 μ€€λΉ„λ₯Ό 도와쀄 수 μžˆλ„λ‘ λͺ‡κ°œμ˜ κΈ°λŠ₯을 μž‘μ„±ν–ˆμ–΄. μΉœκ΅¬λŠ” μ½”λ“œλ₯Ό ✌ν–₯μƒμ‹œν‚€κΈ° μœ„ν•΄βœŒ κΈ°μ—… μ• ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œνŒ€κ³Ό 일해. λ‹€μŒμœΌλ‘œ λ‚΄κ°€ μ•„λŠ”κ±΄, κ·Έ μ‚¬λžŒλ“€μ΄ λ‚΄ νƒ€μž…μŠ€ν¬λ¦½νŠΈ μ½”λ“œμ˜ λ¦¬ν„°λŸ΄κ³Ό μœ λ‹ˆμ–Έ νƒ€μž…μ„ 였래된 순수 μ›μ‹œ νƒ€μž…μœΌλ‘œ λ°”κΏ¨λ‹€λŠ” κ±°μ•Ό.(읽기만 해도 μ—΄λ°›μŒ)
이 μžλ°” νŒ¬λ“€μ€ μ™„μ „ 쒋은 κ°œλ°œμžλ“€μ΄μ•Ό(κ²Œλ‹€κ°€ μ‚¬λž‘μŠ€λŸ¬μ›€). ν•˜μ§€λ§Œ 우린(μžλ°”μ™€ μžλ°”μŠ€ν¬λ¦½νŠΈ) νƒ€μž… μ‹œμŠ€ν…œμ΄ μΌμΉ˜ν•˜μ§€ μ•Šμž–μ•„. μ§€κΈˆ νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ λ‚΄ μ½”λ“œμ—μ„œ νƒ€μž… μ—λŸ¬λ“€μ„ λ³΄κ³ ν•˜κ³  μžˆκ±°λ“ . λ‚΄ νŒŒμΌλ“€μ—μ„œ νƒ€μž… μ• λ„ˆν…Œμ΄μ…˜λ“€μ„ μ˜¬λ°”λ₯΄κ²Œ κ³ μ³μ€„λž˜?

🍽 Step 1: Ingredients

The first area of code I’ll need you to fix is my ingredients planner. I use this to print what kind of salad greens and dressings to use for meals.
Again, the runtime code is working fine. It’s just the type annotations you’ll need to correct.

β–ͺ μš”μ•½: μƒλŸ¬λ“œ 야채와 λ“œλ ˆμ‹± 재료 ν”Œλž˜λ„ˆμ˜ νƒ€μž… μ• λ„ˆν…Œμ΄μ…˜μ„ 고쳐라!

πŸ“„ 문제 μ½”λ“œ

// Please correct any type annotation problems here! ✨
let arugula: number;
let dressing: string;
let lettuce: number;
let mealDate: string;

arugula = 2;
dressing = "honey dijon";
lettuce = undefined;
mealDate = new Date("September 13, 2021");

console.log(`We're starting on ${mealDate} with a dressing of ${dressing}.`);

if (arugula) {
  console.log(`There are ${arugula} arugula serving(s) for this first meal.`);
}

if (lettuce) {
  console.log(`There are ${lettuce} lettuce serving(s) for this first meal.`);
}

arugula = undefined;
dressing = "balsamic vinaigrette";
lettuce = 1;
mealDate = new Date("March 13, 2022");

console.log(`Next up, a ${mealDate} meal with a dressing of ${dressing}.`);

if (arugula) {
  console.log(`This time, there are ${arugula} arugula serving(s).`);
}

if (lettuce) {
  console.log(`This time, there are ${lettuce} lettuce serving(s).`);
}

export {};

μ½”λ“œλ₯Ό 컴파일 ν•˜λ©΄ λ‹€μŒκ³Ό 같은 μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€.

image

πŸ“„ 풀이 κ³Όμ •

μ—λŸ¬ λ©”μ‹œμ§€λ₯Ό 보고 λ³€μˆ˜ μ„ μ–ΈλΆ€μ—μ„œ μœ λ‹ˆμ–Έ νƒ€μž…μœΌλ‘œ νƒ€μž…μ„ ν™•μž₯ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

let arugula: number | undefined;
let dressing: string;
let lettuce: number | undefined;
let mealDate: string | Date;

μ •λ‹΅ μ½”λ“œλ₯Ό λ³΄λ‹ˆ 고쳐야할 점이 μžˆμŠ΅λ‹ˆλ‹€.

let mealDate: Date;

λ³€μˆ˜ mealDateλŠ” ν•˜λ‚˜μ˜ νƒ€μž…λ§Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
λ”°λΌμ„œ μœ λ‹ˆμ–Έ νƒ€μž…μ„ 톡해 νƒ€μž…μ„ ν™•μž₯ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ, νƒ€μž… μ• λ„ˆν…Œμ΄μ…˜μ—μ„œ νƒ€μž…μ„ μˆ˜μ •ν•΄μ£Όλ©΄ λ˜λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€.
λ‚šμ˜€μŠ΅λ‹ˆλ‹€.

🍽 Step 2: Recipes

Those salad ingredients are looking delectable, thank you! Next up is my list of favorite recipes.
It looks like these seem to pass the TypeScript compiler fine as-is. However, there’s a catch: I want to make sure future recipes keep to the same difficulty and group types. Could you please use unions of literal types for them? Both should only have three possible values.
This time, the runtime code is mostly working fine. Except I think I made a typo in one of the group values? You’ll need to fix that. Otherwise it’s just the type annotations you’ll need to correct.

β–ͺ μš”μ•½: μƒλŸ¬λ“œλŠ” 이제 λ˜μ—ˆμœΌλ‹ˆ, 미래의 μš”λ¦¬λ²•(?)이 λ™μΌν•œ λ‚œμ΄λ„μ™€ κ·Έλ£Ή νƒ€μž…μ„ 갖도둝 λ¦¬ν„°λŸ΄ μœ λ‹ˆμ–Έ νƒ€μž…μ„ μ‚¬μš©ν•΄λΌ!

  • μž‘μ„±λ˜λŠ” μœ λ‹ˆμ–Έ νƒ€μž…μ€ μ„Έκ°œμ˜ νƒ€μž…μœΌλ‘œ κ΅¬μ„±λ©λ‹ˆλ‹€.

πŸ“„ 문제 μ½”λ“œ

이번 λ¬Έμ œλŠ” 문제 μ½”λ“œλ₯Ό μ»΄νŒŒμΌν•΄λ„ μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λͺ©μ μ€ μ—λŸ¬ 해결이 μ•„λ‹Œ, μœ λ‹ˆμ–Έ νƒ€μž…μ„ μ‚¬μš©ν•œ μ½”λ“œ λ¦¬νŒ©ν† λ§μž…λ‹ˆλ‹€.

// Please clarify any overly wide (permissive) type annotations here! ✨
let difficulty: number;
let group: string;
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 = "desert";
title = "Biscuits and Coffee";
console.log(`[${group}] ${title}: ${difficulty}/3 difficulty`);

export {};

πŸ“„ 풀이 κ³Όμ •

정해진 값을 μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λŠ” difficulty와 group으둜 λ³΄μž…λ‹ˆλ‹€.

이 λ³€μˆ˜λ“€μ΄ μ‚¬μš©ν•˜λŠ” 값을 λ¦¬ν„°λŸ΄ νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•΄μ„œ μœ λ‹ˆμ–Έ νƒ€μž…μœΌλ‘œ νƒ€μž…μ„ ꡬ체화 μ‹œμΌœλ³΄κ² μŠ΅λ‹ˆλ‹€.

let difficulty: 1 | 2 | 3;
let group: "appetizer" | "entree" | "dessert";
let title: string;

μœ λ‹ˆμ–Έ νƒ€μž…μ„ μž‘μ„±ν•˜λ‹€λ³΄λ‹ˆ 문제 제곡자의 μ˜€νƒ€κ°€ λ³΄μž…λ‹ˆλ‹€.

μ„€λ§ˆ λ ˆμ‹œν”Όμ— β€˜desert(사막)`λ₯Ό λœ»ν•˜λŠ” 단어λ₯Ό μ‚¬μš©ν•˜μ§„ μ•Šμ„ 것 κ°™μ€λ°μš”.

μ •λ‹΅ μ½”λ“œμ—λŠ” dessert둜 μ˜¬λ°”λ₯΄κ²Œ μž‘μ„±λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

λ„ˆλ¬΄ μ‚¬μ†Œν•œ μ‹€μˆ˜λΌ ν•΄λ‹Ή λ ˆν¬μ— prλ₯Ό λ‚ λ €λ³ΌκΉŒ κ³ λ―Όν•˜λ‹€ μ–΄λ–€ μ‚¬λžŒμ€ aν•˜λ‚˜λ‘œλ„ prλ₯Ό μž‘μ„±ν•˜κΈΈλž˜ prλ₯Ό μž‘μ„±ν•΄λ³΄κΈ°λ‘œ κ²°μ‹¬ν–ˆμŠ΅λ‹ˆλ‹€.

μ•„λ‹ˆλ©΄ μ‘°μŠˆμ•„ 골든버그씨가 말해주겠죠.

πŸ–‡ 인생 처음 μ˜€ν”ˆμ†ŒμŠ€ PR

🍽 Step3: Seating

You’re doing wonderfully, my friend. I can’t wait to invite you to my next fancy dinner party.
Speaking of which, I need to properly type the program to randomize invitations and seat assignments. My friends can be kind of childish -they’re babies, really- and are very picky about seating.
I also very much like what you did with the string literal union types in the last step. Could you please avoid using the string type altogether in this one? Just use literal types.
Oh, and I think there’s a typo in one of the names here too.

β–ͺ μš”μ•½: μ €λ²ˆ stepμ—μ„œ λ„€κ°€ λ§Œλ“  string λ¦¬ν„°λŸ΄ μœ λ‹ˆμ–Έ νƒ€μž… λ§˜μ— λ“€μ—ˆμ–΄. μ΄λ²ˆμ—” stringνƒ€μž…μ„ μ‚¬μš©ν•˜μ§€λ§κ³  λ¦¬ν„°λŸ΄ νƒ€μž…λ§Œ μ‚¬μš©ν•΄μ„œ νƒ€μž…μ„ μ™„μ„±μ‹œμΌœλ΄λΌ!

πŸ“„ 문제 μ½”λ“œ

// Please fill in any missing type annotations here...
const headOfTable = "Me!";
let adjacentLeft;
let adjacentRight;
let furtherLeft;
let furtherRight;

// I always invite Susie and Tommy! β™₯
if (Math.random() > 0.5) {
  adjacentLeft = "Susie";
  adjacentRight = "Tommy";
} else {
  adjacentLeft = "Tommy";
  adjacentRight = "Susie";
}

// I invite Angelica about half of the time. We're not as close as Susie and Tommy. It's a long story.
// I try to fill `furtherLeft` first...
if (Math.random() > 0.5) {
  furtherLeft = "Angelica";
}

// Same with Chuckie. I like them, but do I *really* like hanging out with them? Only sometimes.
// ...then after that `furtherRight`
if (Math.random() > 0.5) {
  if (furtherLeft) {
    furtherRight = "Chuckie";
  } else {
    furtherLeft = "Chuckie";
  }
}

// If I invited Angelica but not Chuckie, I'll invite Kimi. They get along well with Angelica but not Chuckie.
if (furtherLeft === "Angelica" && furtherRight !== "Chuckie") {
  furtherRight = "Kimi";
}

// If I invited Chuckie but not Angelica, I'll invite Timmy. They get along well with Chuckie but not Angelica.
if (furtherLeft === "Chuckie") {
  furtherRight = "Timmy";
}

console.log(`At the head of the table is... ${headOfTable}`);

console.log(`Adjacent to the left is: ${adjacentLeft}`);
console.log(`Adjacent to the right is: ${adjacentRight}`);

console.log(`Further down on the left is: ${adjacentLeft ?? "nobody"}`);
console.log(`Further down on the right is: ${adjacentRight ?? "nobody"}`);

export {};

πŸ“„ 풀이 κ³Όμ •

λ³€μˆ˜ μ„ μ–ΈλΆ€λ₯Ό λ³΄λ‹ˆ μ΄ˆκΉƒκ°’μ΄ μ„€μ •λ˜μ–΄ μžˆμ§€ μ•Šμ•„, νƒ€μž…μ΄ any둜 μΆ”λ‘ λ˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

λ³€μˆ˜ μ„ μ–ΈλΆ€μ—μ„œ λ¦¬ν„°λŸ΄ νƒ€μž…μœΌλ‘œ 각각 νƒ€μž…μ„ μ„€μ •ν•©λ‹ˆλ‹€.

let adjacentLeft: "Susie" | "Tommy";
let adjacentRight: "Susie" | "Tommy";
let furtherLeft: "Angelica" | "Chuckie";
let furtherRight: "Kimi" | "Timmy" | "Chuckie";

image

값이 ν• λ‹Ήλ˜κΈ° 전에 μ‚¬μš©λ˜μ–΄ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.

값이 ν• λ‹Ήλ˜μ§€ μ•Šμ€ λ³€μˆ˜λ₯Ό μœ„ν•΄ undefined νƒ€μž…μ„ λͺ…μ‹œμ μœΌλ‘œ μΆ”κ°€ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

let furtherLeft: "Angelica" | "Chuckie" | undefined;
let furtherRight: "Kimi" | "Timmy" | "Chuckie" | undefined;

문제 좜처

Leave a comment