镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

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

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

Activity

  1. fatcerberus commented on Mar 20, 2023

    @fatcerberus

    Hmm, but args is typed as T in this example, which must necessarily be instantiated with a readonly array type, and args is required to be the same type as input… so how would this work?

  2. jakebailey commented on Mar 20, 2023

    @jakebailey
    MemberAuthor

    Like, how would it be implemented? Or something else?

  3. fatcerberus commented on Mar 20, 2023

    @fatcerberus

    in short: how could args not be readonly if input is, since both are typed as T? It seems like you’d need to manufacture a type parameter here.

  4. jakebailey commented on Mar 20, 2023

    @jakebailey
    MemberAuthor

    Well, the idea I (rather Wesley Wigham (@weswigham)) had was to copy Mutable<T> into lib.d.ts and say that within the function, the type of the ...args parameter is always observed as Mutable<T>, dropping readonlyness.

    So, the function is typed the same.

  5. jakebailey commented on Mar 20, 2023

    @jakebailey
    MemberAuthor

    Sorta kinda the same as what I did for spread types in assignments, which implicitly drops certain properties (#47078).

  6. changed the title [-]Inferred ...args should not be readonly[/-] [+]...args should not be readonly within function body[/+] on Mar 21, 2023
  7. RyanCavanaugh commented on Mar 21, 2023

    @RyanCavanaugh
    Member

    Jake Bailey (@jakebailey) feel free to self-assign if you want to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions