Lesson 9 of 10
Function types
- 12 min
- 6 checks
- 3 hints
- Go to task
Goal
Passing functions and optional parameters
Explanation
When a parameter takes a function, its type is a function type, like fn: (n: number) => number: a function that takes a number and returns a number. A function that expects a string is rejected, and when you pass (n) => n * 2, you don't need to write the type of n, because the compiler infers it from the context.
In TypeScript, you must pass exactly as many arguments as the function has parameters. If a parameter can be left out, you have to say so explicitly: note?: string is an optional parameter whose type inside the function is string | undefined, and currency = "SAR" is a default value the compiler also infers the type from.
Keep in mind that a default value only replaces undefined. An empty string "" stays an empty string, so the price shows up with no currency.
Your task
Make the fn parameter of transformAll a function that takes a number and returns a number, give currency the default value "SAR", and make note optional.
Checks
6 checks- No type errors
- The function passed in must accept a number
- The function passed in must return a number
- transformAll applies the function to every element
- currency and note can be left out
- The default currency is "SAR"