A Shelf test helper turns a fee into text. Someone calls it with the two arguments in the wrong order.
function formatFee(amount, currency) {
return amount.toFixed(2) + " " + currency;
}
console.log(formatFee("EUR", 4));
TypeError: amount.toFixed is not a function
That is JavaScript, and the mistake shows only when the line runs. The wording of the error comes from the engine in Node and Chrome. In a test suite the line may sit in a branch that runs once a month.
Here is the same helper with types.
function formatFee(amount: number, currency: string): string {
return amount.toFixed(2) + " " + currency;
}
formatFee("EUR", 4);
fees.ts(5,11): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
The compiler found the wrong call without running anything. It reports the first argument that does not fit.
Reading the first line
Look at line 1 of the helper again and take it one piece at a time.
amount: numbersays the first argument must be a number.currency: stringsays the second must be text.: stringafter the closing bracket is the return type. It says what the function gives back.
The return type checks the body
TypeScript can infer a return type. Writing it yourself adds a check on the function body.
function feeLabel(amount: number): string {
if (amount === 0) {
return "No fee";
}
}
fees.ts(1,36): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
The function promises text. When amount is not 0, it reaches the end and returns nothing, which is the value undefined. Without the : string, TypeScript would infer "text or undefined" and say nothing. The mistake would show up later, in some test that prints undefined.
Reviewers often ask for a return type on every helper that other files use. This check is the reason.
The number of arguments
JavaScript lets you call a function with too few arguments. TypeScript does not.
function formatFee(amount: number, currency: string): string {
return amount.toFixed(2) + " " + currency;
}
formatFee(4);
fees.ts(5,1): error TS2554: Expected 2 arguments, but got 1.
Optional parameters and defaults
Sometimes a caller should be allowed to leave an argument out. Give the parameter a default value. TypeScript infers its type from the default, and a caller may skip it.
function formatFee(amount: number, currency = "EUR"): string {
return amount.toFixed(2) + " " + currency;
}
console.log(formatFee(4));
console.log(formatFee(4, "USD"));
4.00 EUR
4.00 USD
You can also write a question mark after the name: currency?: string. That makes the parameter optional with no default. Inside the function its value may then be undefined, and TypeScript makes you handle that. Lecture 6 shows how.
A function that returns nothing
A helper that only does something, and gives nothing back, has the return type void.
function logFee(amount: number): void {
console.log("Fee: " + amount);
}
A callback has a type too
A callback is a function you pass to another function. Its type is written like an arrow function: the parameters, an arrow, then the return type.
function forEachTitle(titles: string[], action: (title: string) => void): void {
for (const title of titles) {
action(title);
}
}
forEachTitle(["Dune", "Emma"], (title) => console.log(title.toUpperCase()));
DUNE
EMMA
string[] means a list of text. Lecture 7 covers lists. (title: string) => void says that action takes one text argument and returns nothing.
Look at the last line. The arrow function has no annotation on title, and there is no error. TypeScript already knows from forEachTitle what that callback receives. This is why the callbacks you pass to a test framework rarely need annotations.
Remember this
Annotate each parameter and write the return type on helpers that other files use. The compiler then checks every call for the kind and the number of arguments, and checks the body against the return type. A default value makes a parameter optional.
Reference: More on Functions in the TypeScript Handbook.