8.
We have a function below that takes in a varying number of message parameters and outputs them to the console. How can we strongly type the message rest parameter so that only string types can be passed?
function outputMessages(...messages) {
messages.forEach((message) => {
console.log(message);
});
}
No type annotation is needed for the messages rest parameter
We can add the type annotation string[] to the messages rest parameter
We can add the type annotation any to the messages rest parameter
We can add the type annotation string to the messages rest parameter
Correct: B
By adding the type annotation string[] to the messages rest parameter, we ensure that only string types can be passed as arguments to the function. This helps catch potential type errors at compile-time and provides better tooling support.