Beginner's TypeScript Tutorial (18 exercises)

Optional Parameters

Our getName function has been refactored.

Now instead of taking in a single params object, it takes in first and last as individual parameters:

This time TypeScript is telling us that in the first name test that it expected two arguments but only got one:

If we update the first name test to call getName with "Pocock" as a second argument, the error will go away. However, the test would fail at runtime.


Your challenge is to work out how to mark the last parameter as optional.


We've got a similar setup to our previous exercise, but the API is slightly different. Instead of parsing a single object, we're actually parsing two function parameters here.

We're still getting the same problem, which is it should work with just the first name but it's not. It's saying you need to parse in the last parameter.

Here, the error message is a little bit different. It's saying it expected two arguments but got one. If you add in the second property here then, of course, it's going to work but it's going to fail at runtime.

Your challenge is to work out how we can make this argument optional.