Skip to content

readonly function arguments #29742

Closed
Closed
@mohsen1

Description

@mohsen1

Search Terms

readonly parameters, readonly arguments, read only, function

Suggestion

Allow specifying a readonly modifier to function and method arguments

function foo(mutableString: string, readonly immutableString: string) {
    mutableString = 'bar' // ok
    immutableString = 'bar' // not ok
}

With #29435 we can do this for rest arguments too:

function foo(readonly ...immutableStrings: string[]) {}
// the same as
function foo(...immutableStrings: readonly string[]) {}

Use Cases

There is a ESLint rule for not allowing overriding incoming function argument no-param-reassign

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions