Skip to content

Using auto-accessor with readonly or declare should raise an errorΒ #51820

Description

Bug Report

πŸ”Ž Search Terms

auto accessor readonly declare

πŸ•— Version & Regression Information

Version 4.9.3

  • I was unable to test this on prior versions because the auto-accessor feature is new in 4.9

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class Foo {
    accessor readonly foo: number = 5;
    //       ^ no error here
    accessor declare bar: string;
    //       ^ no error here
}

const myc = new Foo();
myc.foo = 6;
// property is not read-only, which is correct because trying to
// define it read-only as above should be an error in the first place

πŸ™ Actual behavior

No TypeScript error is shown, neither for readonly nor for declare.
readonly is completely ignored, i.e. writing to the property also does not result in an error.

πŸ™‚ Expected behavior

In the auto-accessor PR description, it is explicitly stated that

  • accessor cannot be used with readonly or declare on the same field declaration.

So I'd expect a corresponding TypeScript error message for both accessor declarations in the example above.
Once using readonly on an accessor raises an error, it is correct to ignore this for further analysis, so the current behavior to not report an error when trying to write to the property is correct and should remain as-is.

Activity

  1. fatcerberus commented on Dec 8, 2022

    @fatcerberus

    I'm a bit confused why declare accessor wouldn't be legal, but accessor declare is just weird and looks like an oversight in the parser.

  2. RyanCavanaugh commented on Dec 8, 2022

    @RyanCavanaugh
    Member

    Yeah, modifier order is always enforced. This is a bug.

  3. fwienber commented on Dec 12, 2022

    @fwienber
    Author

    That was fast! πŸ‘

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions