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.

Activity

RyanCavanaugh

RyanCavanaugh commented on Feb 5, 2019

@RyanCavanaugh
Member

Duplicate #18497

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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mohsen1@RyanCavanaugh

        Issue actions

          readonly function arguments · Issue #29742 · microsoft/TypeScript