Skip to content

Support property decorators for shorthand constructor-parameter syntax #10777

@lu4

Description

@lu4

TypeScript Version: 2.0.2

Code
I'm taking advantage of shorthand property declarations syntax via constructor.
The code below contains a problem, it won't work as expected for class City, it such case typescript will treat PropertyDecorator as ParameterDecorator, there is no way to supply both PropertyDecorator and ParameterDecorator simultaneously for such shorthand syntax:

function PropertyDecorator(target: Object, propertyKey: string | symbol) { }
function ParameterDecorator(target: Object, propertyKey: string | symbol, index: number) {}

class Country {
    @PropertyDecorator public name: string;

    public constructor(name: string) {
        this.name = name;
    }
}

class City {
    public constructor(
        @PropertyDecorator public name: string,
        @PropertyDecorator public country: Country) {
    }
}

It looks better to use the following syntax that would allow distinguishing different decorator types:

function PropertyDecorator(target: Object, propertyKey: string | symbol) { }
function ParameterDecorator(target: Object, propertyKey: string | symbol, index: number) {}

class Country {
    public name: string;

    public constructor(name: string) {
        this.name = name;
    }
}

class City {
    public constructor(
        @PropertyDecorator public @ParameterDecorator name: string,
        @PropertyDecorator public @ParameterDecorator country: Country) {
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions