Set Properties as Optional
Consider this getName
function:
export const getName = (params: { first: string; last: string }) => {
if (params.last) {
return `${params.first} ${params.last}`;
}
return params.first;
};
Reading the code, we can see that we don't need to pass in a last
name in order for the function to work.
However, TypeScript doesn't know that yet.
We have an error with our "Should work with just the first name" test:
Argument of type '{ first: string; }' is not assignable to parameter of type '{ first: string; last: string; }'.
Property 'last' is missing in type '{ first: string; }' but required in type '{ first: string; last: string; }'.
Challenge
Your challenge is to figure out how to type the object so that last
is optional.
Transcript
Your next challenge is a function called getName here, where you parse in a params object with the first name and the last name here, except you don't need to parse in the last name.
TypeScript doesn't seem to know this yet. It's moaning at me here by saying, "Property 'last' is missing in type blah blah blah but required in this one."
It's telling me I do need to parse the last name here. I don't want to. Figure out a way that you can type this object where last is optional.