Support for auto-accessor fields from the Stage 3 Decorators proposal#49705
Merged
Support for auto-accessor fields from the Stage 3 Decorators proposal#49705
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds support for
accessorfield declarations described in the Stage 3 Decorators proposal.An Auto-Accessor is a field declaration that will be transformed by the runtime into a pair of
getandsetaccessors that access a private backing field:When you use
--target ESNext,accessorfields will be left as is to be transformed by the runtime. Any earlier--targetwill result in TypeScript downleveling theaccessorfield to a compatible runtime implementation.Auto-Accessor fields have several capabilities:
get/setwithout a superclass field potentially shadowing the property during initialization.getandsetaccessor pair and can replace them without changing the runtime shape of the class.In addition, there are several rules around the use of the
accessorkeyword:accessorfields require a minimum of--target ES2015, similar to our support for private identifiers (i.e.,#x) and for the same reasons (a dependency onWeakMap/WeakSet).accessormay only appear in front of field declarations on aclass. It is not supported ininterfaceor object type literals.accessorcannot be used withreadonlyordeclareon the same field declaration.accessorcan be used in an ambient class declaration.accessorfield declarations can be decorated with TypeScript's legacy decorators (i.e., under--experimentalDecorators). They will behave as if you decorated agetorsetdeclaration (i.e., you will receive aPropertyDescriptorat runtime with bothgetandsetfunctions).accessorwill also be marked withaccessorin the output declaration file.accessorfield does not make a class nominal, despite the synthetic private backing field.Symbolfor anaccessorfield is not aSymbolFlags.Property, but rather aSymbolFlags.GetAccessor | SymbolFlags.SetAccessor.NOTE: This is not an implementation of the full Stage 3 Decorators proposal as that effort is still in progress.