Skip to content

...args should not be readonly within function bodyΒ #53398

Open
@jakebailey

Description

@jakebailey

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

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions