TypeScript · Senior · Question 21 of 75
Can you pass a readonly string[] to a function that accepts string[]?
- ✓Correct answer: No: the function could modify the array
- 2Yes:
readonlyis only a hint; compatibility isn't checked - 3Yes, but only with strict mode turned off
Explanation
The reverse direction (string[] → readonly string[]) is allowed. So parameters that the function doesn't modify are better declared as readonly T[] - then it can be called with both mutable and readonly arrays.