Beginner's TypeScript Section 18 exercises
Problem

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:


export const getName = (first: string, last: string) => {
if (last) {
return `${first} ${last}`;
}
return first;
};

T

Loading exercise

Transcript

0:00 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.

0:10 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.

0:19 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.

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