Learn/TypeScript/15. Final project
Lesson 15 of 15
Sign in

TypeScript, lesson 15

Final project

This final lesson brings the course together. Each exercise is a small but complete program. There is nothing new to learn; the skill being practised is design: deciding which types to declare, which functions to write, and how data flows from input to output.

Start with the types

In TypeScript a good first step is to write down the shapes of your data before writing any logic. For a gradebook, that might be:

These few lines already answer many questions: what one input line becomes, what a report line needs, and what each function should accept and return. Once the types exist, the checker guides you as you write the rest; when something does not fit, you find out immediately.

A pipeline of small functions

Organise the program as a pipeline:

  1. Parse raw text into typed values (parseScore(line: string): Score).
  2. Transform with focused functions: group, aggregate, sort.
  3. Format the results into strings, and print at the very end.

Each step has a clear input and output type, so you can write and check them one at a time. Pure functions (no printing, no outside state) are the easiest to get right.

2 exercises to try what you just read.

Exercise 1

Gradebook

Press Ctrl Enter or Check to test your code.

Exercise 2

Task tracker

Press Ctrl Enter or Check to test your code.

Commands as a discriminated union

Programs that read commands map naturally onto a union type, one variant per command:

The parser turns text into a Command (or reports a problem); a switch on type handles each variant, with a never check so no command is forgotten. Invalid input is an expected failure, so returning a result such as Command | string (a command or an error message) or the Result type from the errors lesson is often cleaner than throwing.

State in a class

Long-lived state, like a list of tasks, belongs in a class with private fields and methods that enforce the rules. The rest of the program cannot put the data in an invalid state because it cannot reach the fields directly.

Before you submit

Run through the edge cases yourself: no data at all, a single item, ties when sorting, unknown names, invalid commands. Write the expected output by hand for a tiny input and compare. Most bugs in programs like these hide at the edges, not in the main path.

Further reading

  • TypeScript Handbook: The Basics
  • TypeScript Handbook: Narrowing, discriminated unions
  • TypeScript Handbook: Classes
  • Deno docs: TypeScript support

Each input line is a score: STUDENT SUBJECT POINTS (points are whole numbers from 0 to 100; names and subjects are single words). Print a report:

  1. One line per student, sorted by average (highest first, ties alphabetically by name): NAME avg A grade G best SUBJECT, where A is the average with one decimal, G is the letter grade for the average, and SUBJECT is the subject with the student's highest score (the first one listed in the input if tied).
  2. A line ---.
  3. One line per subject, alphabetically: SUBJECT avg A with one decimal.

Letter grades: A for 90 and above, B for 80 and above, C for 70 and above, D for 60 and above, otherwise F.

Sample input:

mia math 95
leo math 72
mia art 88
leo art 90
zoe math 60

Sample output:

mia avg 91.5 grade A best math
leo avg 81.0 grade B best art
zoe avg 60.0 grade D best math
---
art avg 89.0
math avg 75.7

Build a command-line task tracker. Each input line is a command:

CommandEffect
add TITLE...add a task (the title is the rest of the line); print added #ID
done IDmark the task done; print done #ID
remove IDdelete the task; print removed #ID
listprint every task in id order as #ID [x] TITLE (done) or #ID [ ] TITLE, or no tasks
statsprint D/T done: done tasks over total tasks

Ids start at 1 and increase with every add; ids of removed tasks are never reused.

Errors (print the message and carry on):

  • unknown command word: error: unknown command WORD;
  • add with no title: error: missing title;
  • done/remove with an id that is not a whole number: error: bad id;
  • done/remove with an id that does not exist: error: no task #ID;
  • done on a task that is already done: error: #ID already done.

Sample input:

Sample output:

interface Score {
  student: string;
  subject: string;
  points: number;
}

interface StudentSummary {
  name: string;
  average: number;
  best: Score;
}
type Command =
  | { type: "add"; title: string }
  | { type: "done"; id: number }
  | { type: "list" };
class Counter {
  private next = 1;
  take(): number {
    return this.next++;
  }
}
add buy milk
add call mum
done 1
done 1
remove 7
list
stats
added #1
added #2
done #1
error: #1 already done
error: no task #7
#1 [x] buy milk
#2 [ ] call mum
1/2 done