Back to sessions

Lab handout

L02: Expressions, Values, and Types

Deck: our_typescript_course_decks/lectures/L02 - Expressions, Values, and Types.pptx

Puzzles

  1. Trace baseScore + bonus * 2 when baseScore = 10 and bonus = 3.
  2. Write the type of score, "score", score >= 10, and String(score).
  3. Rewrite level = "3" as the value and type the program actually needs.

Puzzle Stations

Use these as quick hands-on checks. Complete a few before writing code.

  1. Trace: 3 + 4 * 2, (3 + 4) * 2, "score" + 8.
  2. Sort: 42, "42", true, "ready", 0.
  3. Boolean: score >= 10, lives === 0, name.length > 3.
  4. Domain: choose allowed values for difficulty: any string, or "easy" | "normal" | "hard"?
  5. Domain: choose allowed values for lives: any number, or whole numbers from 0 to 3?
  6. Compiler clue: explain why const score: number = "10" fails.
  7. Compiler clue: explain why const ready: boolean = 1 fails.
  8. Compiler clue: explain why const difficulty: "easy" | "normal" | "hard" = "medium" fails.
  9. Repair: fix each failed line without using any.
  10. Review: reject two AI examples whose type or domain is too broad.

Compiler Error Challenge

  • Copy labs/snippets/L02-l02-types-starter.ts.txt to starter/src/l02-types.ts.
  • Run npm run build, read the type errors, then fix the constants.
  • Use at least one union type such as "easy" | "normal" | "hard".

Run If You Touch Code

cd starter
npm install
npm run build
npm test

Common Bugs

  • Guessing a value before tracing the expression.
  • Using a broad type when a smaller domain is known.
  • Ignoring the compiler message instead of reading the expected and actual types.

Optional Short Homework

Open L02 optional homework.

Bonus Puzzles

  • Make a smaller domain with a union type.
  • Find a case where string plus number gives the wrong result.
  • Ask AI for five typed values and reject the broad or invalid ones.

Answer Key

  1. baseScore + bonus * 2 with baseScore = 10 and bonus = 3 gives 16, with type number.
  2. score has the declared type of the variable; "score" is a string literal; score >= 10 is boolean; String(score) is string.
  3. level = "3" stores a string, not a number. If the program needs arithmetic or ordering, use const level: number = 3; if only named levels are allowed, use a union such as type Level = "1" | "2" | "3".
  4. 3 + 4 * 2 is 11; (3 + 4) * 2 is 14; "score" + 8 is the string "score8".
  5. 42 is a number, "42" is a string, true is a boolean, "ready" is a string, and 0 is a number.
  6. score >= 10, lives === 0, and name.length > 3 all produce booleans.
  7. Difficulty should be a small union such as "easy" | "normal" | "hard" if those are the only allowed choices.
  8. Lives should be represented as whole numbers from 0 to 3; TypeScript cannot enforce that exact numeric range by itself without extra modeling or validation.
  9. const score: number = "10" fails because the value is a string but the variable expects a number.
  10. const ready: boolean = 1 fails because 1 is a number, not a boolean. Use true or false.
  11. const difficulty: "easy" | "normal" | "hard" = "medium" fails because "medium" is outside the allowed union.
  12. One valid repair is const score: number = 10, const ready: boolean = true, and const difficulty: Difficulty = "normal".
  13. Reject AI examples that use types like string for known small domains, mix quoted numbers with numbers, or fix errors with any instead of choosing the right value or type.