-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Method/accessor decorators use defineProperty
and getOwnPropertyDescriptor
, which are not allowed in ES3. But class and property decorators don't use them. So class/property decorators can be allowed when targeting ES3.
It would be helpful. E.g. I must support old browsers and I use the property like methods instead of pure properties:
value(v?) {
if (arguments.length) {
// setter
} else {
// getter
}
}
I have the factory function that creates such method:
interface Accessor<T> {
(): T;
(v: T): void;
}
function accessor<T>(propName: string): Accessor<T>;
function accessor<T>(propName: string) {
return function (v?: T) {
// if (arguments.length) and bla-bla-bla
}
}
Currently I have to set such properties in the class prototype explicitly:
class Foo {
value: Accessor<string>;
}
Foo.prototype.value = accessor<string>("value");
With decorators I could write more cleaner code:
function Accessor(proto, propName: string) {
proto[propName] = accessor(propName);
}
class Foo {
@Accessor
value: Accessor<string>;
}
Also there is no need to duplicate the property name ("value"
in the example above).
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript