Open
Description
Bug Report
π Search Terms
readonly parameter inferred tuple array args variadic
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
declare function callFn<T extends readonly any[]>(args: T, fn: (...args: T) => void): void;
declare const input: readonly string[];
callFn(input, (...args) => {
args; // readonly string[] ?
})
declare function callFnNonGeneric(args: readonly string[], fn: (...args: readonly string[]) => void): void;
callFnNonGeneric(input, (...args) => {
args; // readonly string[] ?
})
π Actual behavior
...args
is readonly.
π Expected behavior
...args
is not readonly.
When this function is called, the input will be a brand new array which can be modified without affecting the caller:
> const fn = (...args) => { args[0] = "oops" }
undefined
> const arr = ["some", "values"]
undefined
> fn(...arr)
undefined
> arr
[ 'some', 'values' ]
Split out of #53258 (comment)
The "fix" here is to strip readonly from variadic args (at the top level). This also means the fix in #53258 can be reverted (as the real fix can be more general).