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

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

Closed
fwienber opened this issue Dec 8, 2022 · 3 comments Β· Fixed by #51824
Closed

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

fwienber opened this issue Dec 8, 2022 · 3 comments Β· Fixed by #51824
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@fwienber
Copy link

fwienber commented Dec 8, 2022

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.

@fatcerberus
Copy link

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.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Dec 8, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 8, 2022
@RyanCavanaugh
Copy link
Member

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

@fwienber
Copy link
Author

That was fast! πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants