Deck: our_typescript_course_decks/lectures/L02 - Expressions, Values, and Types.pptx
Puzzles
- Trace
baseScore + bonus * 2whenbaseScore = 10andbonus = 3. - Write the type of
score,"score",score >= 10, andString(score). - 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.
- Trace:
3 + 4 * 2,(3 + 4) * 2,"score" + 8. - Sort:
42,"42",true,"ready",0. - Boolean:
score >= 10,lives === 0,name.length > 3. - Domain: choose allowed values for difficulty: any string, or
"easy" | "normal" | "hard"? - Domain: choose allowed values for lives: any number, or whole numbers from 0 to 3?
- Compiler clue: explain why
const score: number = "10"fails. - Compiler clue: explain why
const ready: boolean = 1fails. - Compiler clue: explain why
const difficulty: "easy" | "normal" | "hard" = "medium"fails. - Repair: fix each failed line without using
any. - Review: reject two AI examples whose type or domain is too broad.
Compiler Error Challenge
- Copy
labs/snippets/L02-l02-types-starter.ts.txttostarter/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 testCommon 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
baseScore + bonus * 2withbaseScore = 10andbonus = 3gives16, with typenumber.scorehas the declared type of the variable;"score"is a string literal;score >= 10isboolean;String(score)isstring.level = "3"stores a string, not a number. If the program needs arithmetic or ordering, useconst level: number = 3; if only named levels are allowed, use a union such astype Level = "1" | "2" | "3".3 + 4 * 2is11;(3 + 4) * 2is14;"score" + 8is the string"score8".42is a number,"42"is a string,trueis a boolean,"ready"is a string, and0is a number.score >= 10,lives === 0, andname.length > 3all produce booleans.- Difficulty should be a small union such as
"easy" | "normal" | "hard"if those are the only allowed choices. - Lives should be represented as whole numbers from
0to3; TypeScript cannot enforce that exact numeric range by itself without extra modeling or validation. const score: number = "10"fails because the value is a string but the variable expects a number.const ready: boolean = 1fails because1is a number, not a boolean. Usetrueorfalse.const difficulty: "easy" | "normal" | "hard" = "medium"fails because"medium"is outside the allowed union.- One valid repair is
const score: number = 10,const ready: boolean = true, andconst difficulty: Difficulty = "normal". - Reject AI examples that use types like
stringfor known small domains, mix quoted numbers with numbers, or fix errors withanyinstead of choosing the right value or type.