Skip to content

Support for auto-accessor fields from the Stage 3 Decorators proposal#49705

Merged
rbuckton merged 3 commits intomainfrom
accessor-fields
Sep 12, 2022
Merged

Support for auto-accessor fields from the Stage 3 Decorators proposal#49705
rbuckton merged 3 commits intomainfrom
accessor-fields

Conversation

@rbuckton
Copy link
Copy Markdown
Contributor

@rbuckton rbuckton commented Jun 28, 2022

This PR adds support for accessor field 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 get and set accessors that access a private backing field:

class C {
  accessor x = 1;
}
// is transformed into
class C {
  // actual field is named `#x accessor storage` so that it isn't user-reachable
  #x_accessor_storage = 1;
  get x() { return this.#x_accessor_storage; }
  set x(value) { this.#x_accessor_storage = value; }
}

When you use --target ESNext, accessor fields will be left as is to be transformed by the runtime. Any earlier --target will result in TypeScript downleveling the accessor field to a compatible runtime implementation.

Auto-Accessor fields have several capabilities:

  • Allows subclasses to override the get/set without a superclass field potentially shadowing the property during initialization.
  • A decorator applied to an auto-accessor receives the get and set accessor pair and can replace them without changing the runtime shape of the class.
  • Native ECMAScript decorators will be able to replace the initializer.

In addition, there are several rules around the use of the accessor keyword:

  • accessor fields require a minimum of --target ES2015, similar to our support for private identifiers (i.e., #x) and for the same reasons (a dependency on WeakMap/WeakSet).
  • accessor may only appear in front of field declarations on a class. It is not supported in interface or object type literals.
  • accessor cannot be used with readonly or declare on the same field declaration.
  • accessor can be used in an ambient class declaration.
  • accessor field declarations can be decorated with TypeScript's legacy decorators (i.e., under --experimentalDecorators). They will behave as if you decorated a get or set declaration (i.e., you will receive a PropertyDescriptor at runtime with both get and set functions).
  • A field explicitly marked with accessor will also be marked with accessor in the output declaration file.
  • An accessor field does not make a class nominal, despite the synthetic private backing field.
  • The TypeScript Symbol for an accessor field is not a SymbolFlags.Property, but rather a SymbolFlags.GetAccessor | SymbolFlags.SetAccessor.

NOTE: This is not an implementation of the full Stage 3 Decorators proposal as that effort is still in progress.

Loading
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants