TS1273
'readonly' modifier cannot appear on a type parameter.
Broken Code ❌
function logValues<readonly T>(values: T[]) {
console.log(values);
}This error occurs because the readonly modifier is not allowed on a type parameter. Type parameters are meant to be used for generic types and should not have modifiers like readonly.
Fixed Code ✔️
You can apply readonly to the type of the array rather than the type parameter itself:
function logValues<T>(values: readonly T[]) {
console.log(values);
}In this fixed version, the array values is considered a read-only array, and its elements cannot be modified within the function. It is a good practice to avoid accidential modifications.
