Beginner · 10 min
Typed functions
Describe function parameters and return values.
Type the inputs
Parameter annotations describe what callers may supply. In strict mode, a parameter without an inferred or explicit type is an error.
function double(value: number): number {
return value * 2;
}
console.log(double(4));Output
8
Describe the result
A return annotation helps detect an accidental change in a function result. A default parameter also gives TypeScript information about its type.
function greet(name: string, greeting = "Hello"): string {
return greeting + ", " + name;
}
console.log(greet("Ada"));Output
Hello, Ada
Put it into practice
- Read the types and predict the output before running the examples.
- Complete the task, then check your understanding with the practice questions.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Return a numeric total
Make "total" return the price multiplied by the quantity. Keep its return type as number.
Return the numeric product directly.