Mutability 12 exercises

Prevent Array Mutation in TypeScript

In this exercise, you'll be dealing with a function called printNames. This function accepts an array of names and prints each name to the console.

However, we want to add a special restriction to this function: it shouldn't be possible to mutate the array it receives. In other words, operation

Loading exercise


00:00 In this exercise, we have a printNames function, which takes in an array of names, which are each string. And it says for const name of names, console.log that name.

00:09 But I want to add basically a constraint on this function to say that you can't push to this array and you also can't modify any of the parts inside it. So you can't say the first name is Billy, first name is Jimmy, whatever. So this function here, printNames, should essentially never touch any of the names.

00:30 And I think that's a reasonable constraint. Your job here is to try to figure out what type you need to give this parameter of names here to basically say, OK, this array is read-only, cannot mutate it. And there are two solutions here. Good luck.