0 of 5 problems solved•0%
5 of 5

Catching Mistakes with Parameter Types

In TypeScript, function parameters should have type annotations. This makes sure you don’t pass the wrong type of value to a function.

Example:

// 👇 :string is a type annotation
function greet(firstName: string) {
  return "Hello " + firstName;
}

One of TypeScript’s biggest benefits is catching errors before you run your code.

Here’s a classic example: a function expects a number, but you accidentally pass a string. In JavaScript, this might run but give wrong results. In TypeScript, you get an error right away.

Your Task

  1. Create a function double that takes a parameter n of type number and returns n * 2.
  2. Call it correctly once with a number.
  3. Try calling it once with a string (keep this line, so you can see the error in the editor).
  4. Log the correct call's result.