-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Accessors should be allowed to be optional #54240
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
Comments
Are you talking about accessors in general? If so then this duplicates #14417. Are you only talking about auto accessors? If so then you might want to edit to use that term specifically. Of course it's still possible they'd consider this subsumed by #14417. |
See also #16344, a PR which sought to implement this but seems to have fallen through (itβs now closed). |
@rbuckton Any thoughts? |
We just finished porting Lit's decorators to the new spec, and started porting some consumers via a code mod. The lack of optional auto accessors is standing out as a DX wart. Any chance this could be considered @rbuckton? |
If you are already porting via a code mod, is there a reason that code mod cannot also drop the optionality and add Field optionality existed long before the native fields proposal, when it was completely feasible to declare an optional field and never actually define it (thus |
The code mod can, but not everyone's going to use the code mode, and new code won't. We have 10s to 100s of thousands of users. For newly written code, the users still have to write: @property() accessor foo: string | undefined; when previously they were writing: @property() foo?: string; Every additional required keyword or boilerplate adds up
I'm not sure how other developers think of |
As I port more decorator usage to standard decorators, the extra cost and keyword clutter of When most people conceptually just want to write: foo?: string; but they need to use a decorator for reactivity, so they must do: @reactive() foo?: string; But decorators only work with auto-accessors, so they must do: @reactive() accessor foo?: string; But accessors aren't allowed to be optional, so they finally get to: @reactive() accessor foo: string | undefined; That's just a lot of extra boilerplate. The kind that honestly leads some people to JS forks. Anything to reduce the sheer number of characters here would help, and optional accessors is basically the one thing within TypeScript's control. |
CL created by running the following script: python3 ui/webui/resources/tools/codemods/jscodeshift.py \ --transform ui/webui/resources/tools/codemods/389737066_migration_lit.js \ --files `find ui/webui/resources/cr_components/theme_color_picker/ -name '*.ts'` Also updated 389737066_migration_lit.js codemod to handle the case of optional class members, to work around [1]. [1] microsoft/TypeScript#54240 Bug: 389737066 Change-Id: Ia7a886a94af2ee0bd32dda7f4c6edd458dd671b4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6341751 Reviewed-by: Rebekah Potter <[email protected]> Commit-Queue: Demetrios Papadopoulos <[email protected]> Cr-Commit-Position: refs/heads/main@{#1431107}
Suggestion
Right now accessors are barred from being declared optional. This is a DX regression for decorator users.
π Search Terms
accessor optional
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Allow accessors to be declared options;
π Motivating Example
Decorating a class field should require as few changes over a non-decorated field as necessary. Disallowing accessors from being optional adds an additional change.
Take a plain class:
Where you then want to make
foo
reactive with some library that vends decorators. This would be the change I would expect to make:But 5.0 requires:
I don't see a semantic problem with optional accessors. They behave very similar to optional fields (with standard field semantics). With
useDefineForClassFields
and this example:both
foo
andbar
would exist on the runtime object even though they're optional, ie'foo' in c === 'bar' in c
.π» Use Cases
More ergonomic decorator usage.
The text was updated successfully, but these errors were encountered: