Skip to content

Partial support decorators when targeting ES3 #4681

Closed
@Koloto

Description

@Koloto

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

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions