TypeScript, lesson 6
A function packages a piece of work under a name. In TypeScript, a function's parameters carry types, so every call is checked: pass the wrong kind of value and the editor tells you straight away.
Parameters need annotations. Unlike variables, there is no value for TypeScript to infer
them from. The return type (: number after the parentheses) is optional, because TypeScript
can infer it from the return statements, but writing it documents your intent and catches
mistakes such as forgetting to return on one branch.
A function that returns nothing has the return type void:
Arrow functions are a shorter syntax, usually stored in a const:
Mark a parameter optional with ?. Inside the function its type then includes , and
the checker makes you deal with that:
2 exercises to try what you just read.
undefinedA default value is often simpler. The parameter becomes optional for callers, but inside the function it always has a value:
percent is inferred as number from its default.
Functions are values, so they have types too. A function type looks like an arrow function without a body:
When you pass a function to another function, TypeScript uses the expected type to infer the parameters of your arrow function. This is called contextual typing, and it is why callbacks rarely need annotations.
Parameters and variables declared inside a function exist only inside it. Code inside a function can read variables from outside, but outer code cannot see in. Functions that depend only on their parameters and return a result, without printing or changing outside variables, are the easiest to test and reuse. Keep the printing in one place, near the end of the program.
Write a function bmi(weightKg: number, heightM: number): number that returns weight divided
by height squared, and a function category(value: number): string that returns:
underweight below 18.5,normal from 18.5 up to (not including) 25,overweight from 25 up to (not including) 30,obese from 30.The program around your functions is already written. It reads lines like 70 1.75 and prints
BMI 22.9 (normal).
Sample input:
Sample output:
Write a function perPerson(total: number, people: number, tipPercent = 10): number that
returns what each person pays: the total plus the tip, divided by the number of people.
Each input line holds a bill total, the number of people and, optionally, a tip percentage.
For each line print Each pays X with two decimals. When the tip is missing, rely on the
default value rather than writing 10 again in your main code.
Sample input:
Sample output:
function area(width: number, height: number): number {
return width * height;
}
console.log(area(3, 4)); // 12
area("3", 4); // error: 'string' is not assignable to 'number'
area(3); // error: Expected 2 arguments, but got 1.function banner(text: string): void {
console.log(`*** ${text} ***`);
}const double = (n: number): number => n * 2;
const greet = (name: string) => {
const message = `Hi, ${name}`;
return message; // inferred return type: string
};function fullName(first: string, last?: string): string {
return last ? `${first} ${last}` : first;
}function withTip(bill: number, percent = 15): number {
return bill * (1 + percent / 100);
}
withTip(40); // 46
withTip(40, 20); // 48type Rule = (score: number) => boolean;
const passed: Rule = (score) => score >= 50; // score is inferred as number
console.log(passed(72)); // true70 1.75BMI 22.9 (normal)100 4
60 3 20Each pays 27.50
Each pays 24.00