-
Notifications
You must be signed in to change notification settings - Fork 105
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
Add a placement: 'none' for conditional properties #85
Comments
We explored another API for this sort of thing in #44 . That issue uses |
As in the const when = condition => descriptor => {
if (!condition) {
descriptor.key = undefined;
}
} I don't think it would make a huge difference which was included as the use cases I'd imagine for them would be covered by either proposal. Although I do think including some mechanism would be preferable as the first example would actually be impossible as outside of the class this would just fail: class MyComponent extends HTMLElement {
...
}
if (isTest) {
Object.defineProperty(MyComponent.prototype, 'internals', {
get() {
return {
// SyntaxError: !
shadowRoot: this.#shadowRoot,
}
},
})
} Certainly the situation isn't dire, one could easily just have |
One use case that the proposal in this issue wouldn't cover, but the one in #44 would cover, is executing some code per instance. That doesn't come up in your example, though. |
We discussed this possible feature in a call among decorators champions. @wycats argued that it should not be in the initial decorators proposal, as we should be generally encouraging decorators authors to use decorators for things which will end up adding something related to what the decorated field or method is doing. For this reason, the feature is likely to wait until a follow-on proposal. |
Another mitigating factor is that class decorators can remove class elements. So, for example, the |
I just came across a use case for this...I'm playing around with lit-element and trying to get their The most straightforward way to update this decorator to work with the current proposal is to use a class finisher: export function property(options) {
return elementDescriptor => ({
...elementDescriptor,
finisher(klass) {
klass.createProperty(elementDescriptor.key, options)
}
})
} ...but that isn't sufficient because the instance property conflicts with what I'm guessing that this use case isn't sufficient to motivate changing the plan of addressing this in a follow-on proposal, but I thought I'd share it anyway since it's a real-world example. Arguably a more proper solution here is to not call OTOH, I think this is a resaonble argument for waiting on this feature:
|
Currently if
placement
is not one of"static"
,"prototype"
or"own"
then decoration throws an error. It would be nice if we could have conditional decorators that can avoid creating the property at all given some condition.Example: expose hidden values when doing tests.
Example 2. progressive feature detection without
Object.defineProperty
The text was updated successfully, but these errors were encountered: