Skip to content

Conversation

sorvell
Copy link
Member

@sorvell sorvell commented Sep 24, 2021

Fixes #462

@sorvell sorvell requested a review from arthurevans September 24, 2021 02:29
@github-actions
Copy link

github-actions bot commented Sep 24, 2021

@sorvell sorvell requested a review from aomarks September 24, 2021 02:30
Copy link
Member

@aomarks aomarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I just have a few suggestions for the "Avoiding issues" section.

[Class fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields) have a problematic interaction with declaring reactive properties. Class fields are defined on the element instance. Reactive properties are defined as accessors on the element prototype. According to the rules of JavaScript, an instance property takes precedence over and effectively hides a prototype property. This means that reactive property accessors do not function when class fields are used. When a property is set, the element does not update.

</div>
**You can use class fields** with reactive properties when transpiling code with Babel or TypeScript and the proper settings are enabled. Both Babel and TypeScript support transpiling class fields into properties set in the constructor rather than defined on the instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest removing this paragraph, kinda seems like it's mostly redundant with the next two paragraphs, which are more focused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


To use decorators with [TypeScript](https://www.typescriptlang.org/docs/handbook/decorators.html), enable the `experimentalDecorators` compiler option.

You should also ensure that the `useDefineForClassFields` setting is `false`. Note, this should only be required when the `target` is set to `es2020` or greater, but it's recommended to explicitly ensure this setting is `false`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's only for esnext, not es2020 (microsoft/TypeScript#34787)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@sorvell sorvell requested a review from aomarks September 24, 2021 15:24
Copy link
Member

@aomarks aomarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Copy link
Member

@aomarks aomarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we can also now update the /msg/class-field-shadowing short link to point to this new section!

['/msg/class-field-shadowing', '/docs/components/properties/#declare'],

@sorvell sorvell requested a review from aomarks September 24, 2021 17:24
Copy link
Contributor

@arthurevans arthurevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, a couple of wording suggestions.

### Using decorators with Babel { #decorators-babel }

If you're compiling JavaScript with [Babel](https://babeljs.io/docs/en/), you can enable decorators by adding the following plugins:
If you're compiling JavaScript with [Babel](https://babeljs.io/docs/en/), you can enable decorators by adding the following plugins and assumptions:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using "assumptions" here is super jargony. You'd have to be pretty well-versed in Babel to know that there are config options called "Compiler assumptions," and the average user is likely to infer the generic meaning for assumptions here.

Suggest either just go with "plugins" and assume (heh) that the configuration steps that follow are part of adding the plugins, or say, "plugins and settings"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to "plugins and settings"

@sorvell sorvell merged commit 7ca04b7 into main Sep 24, 2021
@sorvell sorvell deleted the class-fields branch September 24, 2021 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move class field discussion from Decorators to Reactive properties

3 participants