-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Allow accessors in ambient class declarations #32787
Conversation
@weswigham, @RyanCavanaugh, @sandersn, @ahejlsberg, @DanielRosenwasser for review since GitHub's suggested reviewers still don't seem to be working. |
7b17de1
to
3d7d144
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see this expanded to ObjectLiteral
and Interface
types in the near future, but I guess it's fine to start here until we determine how implementing those is going to work. I can't really come up with a reason reason for not allowing it in classes beyond "we liked the idea of deleting more information in the declaration file".
That's what #32772 does, but after discussing with @RyanCavanaugh we decided to postpone that until at least 3.7. |
Co-Authored-By: Wesley Wigham <wewigham@microsoft.com>
As one of the people getting this error in a d.ts file on npm...
...I'm wondering why it is necessary to "issue error messages when you define a field on a superclass and an accessor on a subclass with the same name (or vice versa)" and why this breaking change was done during a point-release change (3.7) instead of a major version change (4.0). (Plus, what's an "ambient context"?) |
Because TS isn't semver and every minor version of TS contains breaks to some degree, just not big ones.
Declaration file or "declare"'d statement.
Accessors are on the prototype, TS old fields were on the actual instance; this mismatch could lead to occasionally surprising behavior (esp with |
This also fixed #9907. |
As part of the effort to support ECMAScript class fields, we will need to roll out features to the TypeScript compiler in phases. One of the key areas we need to address is the ability to properly distinguish between a "field" (a.k.a. a "Property Declaration" in TypeScript parlance), and an "accessor" (a getter or setter declaration). The reason we need to have these definitions cleanly separated is so that we can eventually issue error messages when you define a field on a superclass and an accessor on a subclass with the same name (or vice versa).
However, this is problematic today for several reasons:
readonly
property declarations:This PR seeks to address some of these issues:
This PR does not change the following behavior:
We are not changing the declaration emit for getters/setters immediately so that declaration files built against existing TypeScript code prior to this feature can still be used with earlier versions of the TypeScript compiler. We do plan to change the declaration emit in a future release, which will give users a transitional period where they can become accustomed to the new syntax.
Examples:
Supersedes #32772
Related #27644
Fixes #19703