CricketScript Lesson 9 of 11 · How they're out  |  Course

How they're out.

A fixed set of named outcomes — that's an enum.

Every dismissal in cricket has a name. Bowled. LBW. Caught. Run out. Stumped. The scorer doesn't invent new ones. The book has its categories, and every wicket falls into one of them.

An enum in TypeScript is exactly that — a fixed list of named members under a single type. Use the name (Dismissal.Bowled) instead of a raw number; the code reads itself, and the compiler still knows it's one of a known set.

Tap a dismissal. Watch the wicket fall, and the scorer write it down by name.

Tap a dismissal. The book has five.

Five members. One type. Names you can read.

An enum binds a fixed set of names to a single type. By default each member gets a number — Bowled is 0, LBW is 1, and so on. But callers write the name, not the number.

enum Dismissal {
  Bowled,
  LBW,
  Caught,
  RunOut,
  Stumped,
}

function recordOut(d: Dismissal): string {
  return `Out: ${Dismissal[d]}`;
}

recordOut(Dismissal.Bowled);   // 'Out: Bowled'
recordOut(Dismissal.Caught);   // 'Out: Caught'

Names beat magic numbers.

Without the enum, you'd write recordOut(2) and hope the reader knows 2 means caught. With it, you write Dismissal.Caught — the meaning is in the call site, not in a comment.

Reach for an enum when a value belongs to a small, named set that won't grow much. Type aliases work too — but enums shine when the names matter as much as the values.

Enjoyed this? Nine more lessons plus a bonus await.

This is one of two free preview lessons — Lessons 1 and 9. The other 9 lessons + the bonus finale unlock with the full course.

$15worldwide · ₹625India (50% off)
Unlock the rest of the course →
30-day money-back guarantee · Lessons 1 and 9 stay free either way
Made by one person, not a company. Questions: [email protected]