Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jakebailey opened this issue Mar 20, 2023 · 6 comments
Open

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

jakebailey opened this issue Mar 20, 2023 · 6 comments
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@jakebailey
Copy link
Member

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

@fatcerberus
Copy link

fatcerberus commented Mar 20, 2023

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?

@jakebailey
Copy link
Member Author

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

@fatcerberus
Copy link

fatcerberus commented Mar 20, 2023

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.

@jakebailey
Copy link
Member Author

jakebailey commented Mar 20, 2023

Well, the idea I (rather @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.

@jakebailey
Copy link
Member Author

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

@jakebailey jakebailey changed the title Inferred ...args should not be readonly ...args should not be readonly within function body Mar 21, 2023
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements labels Mar 21, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 21, 2023
@RyanCavanaugh
Copy link
Member

@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
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants