Skip to content

Readonly functions arguments #41799

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

Closed
5 tasks
antoine-pous opened this issue Dec 3, 2020 · 1 comment
Closed
5 tasks

Readonly functions arguments #41799

antoine-pous opened this issue Dec 3, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@antoine-pous
Copy link

Search Terms

  • readonly function arguments
  • read only function arguments
  • readonly arguments
  • read only arguments

Suggestion

As asked in two other tickets (#29742, #18497), a way to make functions arguments immutable would be really awesome. The proposal was rejected due to potential different behavior by updating the readonly modifier, which can be understood easily.

But in fact this feature can be probably added with an option to enable/disable it through tsconfig.json and then taking the time to get developers feedback about it.

A breaking change is not something bad if it improve the code quality and this feature would be really more useful than using ESLint rule for this purpose. It let you plan it quietly for the next major version and allow devs to adapt their code to deal with it.

If it's implemented i would love having an optional parameter to enforce arguments immutability without having to use explicitly readonly modifier everywhere.

Use Cases

Examples

Actual behavior

const testFunction: number = (mutableArg: number) => mutableArg++ // ok

Expected behavior

const testFunction: number = (readonly immutableArg: number) => immutableArg++ // throw error

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Dec 3, 2020

I don't see a reason for #18497 to have been closed, so I've reopened it.

That said, this can be implemented by a lint rule (e.g. make all params consty by default, have an opt-out through some mechanism), so the bar is a bit higher in terms of whether we would do it or defer to eslint.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants