TypeScript for real code · Chapter 5 of 36
Functions with types
By the end of this chapter you can type a function's optional, default and rest parameters, write the type of a function itself, and say when a function returns nothing or never returns. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Why types
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
Meera wants offers for the monsoon: ₹10 off everything when it rains, half price on bun maska after 9 pm. Bunty wrote them as a list of functions typed Function. One of his rules returned the text 'free' instead of a number, and on the first rainy evening every bill read ₹NaN. The order screen also needs lines with an optional note, and a bill total for any number of prices.
So far a parameter is required and has one type, and a function's result is a plain value. That cannot say "the quantity may be left out, and is 1 then", or "any number of prices", or "a function from a price to a price". Function says only "something you can call": it accepts any arguments, returns any, and lets the 'free' rule straight in.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
chaiLine: a default quantity and an optional note
chaiLine writes one line of an order: chaiLine('Masala chai') is '1 x Masala chai', chaiLine('Cutting chai', 3) is '3 x Cutting chai', and chaiLine('Masala chai', 2, 'less sugar') is '2 x Masala chai (less sugar)'.
Give qty a default of 1 and make note optional, and show the note in brackets only when it is given. name stays required, so chaiLine() is a type error, and so are a quantity written as text, chaiLine('Masala chai', '2'), and a note that is not text, chaiLine('Masala chai', 2, true). It returns a string.
- Task 2
billTotal: any number of prices
billTotal adds up the prices it is given, however many: billTotal(40, 25, 30) is 95 and billTotal() is 0.
typecheck says the rest parameter implicitly has an any[] type. Give it a type, a list of numbers, so a price written as text, like billTotal(40, '25'), is a type error. It returns a number.
- Task 3
PriceRule: a function from a price to a price
Meera's offers are price rules: each takes a price and returns the new price. applyRules(100, [monsoonOff, half]) is 45 and applyRules(40, []) is 40.
Function says nothing about that. Write PriceRule as a function type that takes a price (a number) and returns a number. So a rule that returns text, like (price) => 'free', a plain number like 10, and a function that takes a name are all type errors. Leave the offers and applyRules as they are.
- Task 4 · interview question
Interview: failOrder never returns
failOrder stops an order that cannot be made. It must throw an Error with the reason as its message, and its return type must be never: failOrder('out of milk') throws 'out of milk'. Then priceOf compiles, because TypeScript knows the end of priceOf is never reached: priceOf('small') is 25 and priceOf('jumbo') throws 'no jumbo cups'.
Fix it in failOrder only: leave priceOf as it is. Dev will ask why priceOf does not compile, and how the return type of failOrder fixes it.
What you will learn
- Optional, default and rest parameters: note?: string, guests = 2 and ...ids: string[].
- Function types: (amount: number) => string, the type of a function value, so a list of rules can be checked.
- void and never: a function that returns nothing useful, and one that never returns at all.
More in Why types
Build "Functions with types" yourself
Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.