Question presented to candidate:
"What are ReadonlyArray and readonly tuples in TypeScript? How do they behave when you pass them to functions, and do they actually stop the array from changing?"
What a strong answer should cover:
ReadonlyArray<T>,readonly T[]andReadonly<T[]>are the same type: an array view with no mutating methods (push,sort,splice), no index writes and a read-onlylengthreadonly [A, B]combines a tuple's fixed positions with the same read-only rules;as conston an array literal produces one- Assignability is one-way: a mutable array can be passed where a readonly one is expected, but not the reverse (
'readonly' and cannot be assigned to the mutable type) - API design: accept
readonly T[]parameters so callers can pass both kinds, and constrain generics withreadonly unknown[] - It is compile-time only: the run-time value is an ordinary array, a mutable alias can still change it, and
Object.freezeis the run-time counterpart - Version facts: the
readonly T[]syntax, readonly tuples andas constarrived in TypeScript 3.4
Code / implementation expected: Optional β a function taking readonly number[], plus one rejected push or index assignment.