Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upProperty decorator documentation is inaccurate? #32395
Comments
This comment has been minimized.
This comment has been minimized.
var __decorate = (this && this.__decorate) || function(decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else
for (var i = decorators.length - 1; i >= 0; i--)
if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}; The return value is indeed not ignored here, and is in fact used as input for a call to However, a potential pitfall I see: the decorated property ends up being defined on the prototype and is no longer an instance property because: __decorate([
logProperty
], Greeter.prototype, "greeting", void 0); If you add an initializer to the |
This comment has been minimized.
This comment has been minimized.
Yes, I'm working on a new article which explains how it works under the hood. That's how I found this behavior. I don't think it's a pitfall. It seems like the intended behavior. |
This comment has been minimized.
This comment has been minimized.
I meant "pitfall" in the sense of being a footgun, people might not expect instance properties to get moved to the prototype just because they added a decorator |
This comment has been minimized.
This comment has been minimized.
It's been added to the prototype anyway: __decorate([
logProperty
], Greeter.prototype <=====, "greeting", void 0); |
This comment has been minimized.
This comment has been minimized.
Right, that's what I said - it only goes to the prototype if you have a decorator, otherwise it's strictly an own property of the instance. Seemed surprising, even if it's intentional. |
This comment has been minimized.
This comment has been minimized.
Agree. They explain why in the docs: "This is because there is currently no mechanism to describe an instance property when defining members of a prototype, and no way to observe or modify the initializer for a property. " |
This comment has been minimized.
This comment has been minimized.
Great find @NetanelBasal |
From the official docs about property decorators:
"The return value is ignored too."
But the following example returns a new descriptor, and it works fine: