TypeScript, lesson 15
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.
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.
Organise the program as a pipeline:
parseScore(line: string): Score).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.
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.
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.
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.
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:
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).---.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 60Sample 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.7Build a command-line task tracker. Each input line is a command:
| Command | Effect |
|---|---|
add TITLE... | add a task (the title is the rest of the line); print added #ID |
done ID | mark the task done; print done #ID |
remove ID | delete the task; print removed #ID |
list | print every task in id order as #ID [x] TITLE (done) or #ID [ ] TITLE, or no tasks |
stats | print 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):
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
statsadded #1
added #2
done #1
error: #1 already done
error: no task #7
#1 [x] buy milk
#2 [ ] call mum
1/2 done