[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 {};
μ½λλ₯Ό μ»΄νμΌ νλ©΄ λ€μκ³Ό κ°μ μλ¬κ° λ°μν©λλ€.
π νμ΄ κ³Όμ
μλ¬ λ©μμ§λ₯Ό λ³΄κ³ λ³μ μ μΈλΆμμ μ λμΈ νμ μΌλ‘ νμ μ νμ₯ν΄μ£Όμμ΅λλ€.
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";
κ°μ΄ ν λΉλκΈ° μ μ μ¬μ©λμ΄ μλ¬λ₯Ό λ°μμν΅λλ€.
κ°μ΄ ν λΉλμ§ μμ λ³μλ₯Ό μν΄ undefined
νμ
μ λͺ
μμ μΌλ‘ μΆκ°ν΄μ£Όμμ΅λλ€.
let furtherLeft: "Angelica" | "Chuckie" | undefined;
let furtherRight: "Kimi" | "Timmy" | "Chuckie" | undefined;
Leave a comment