question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support property decorators for shorthand constructor-parameter syntax

See original GitHub issue

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) {
    }
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:11
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aeoncleansecommented, Jan 5, 2022

This would be really handy, and I don’t understand why this issue seems to have deadended at “But it would be ambiguous still”. Well, fine, come up with a special decorator syntax alias for use explicitly in this situation.

Since parameter decoration is the more common within a function declaration, even a constructor, we leave that the default.

To allow the use of decorators for the shorthand declaration, how about !@Prop or @@Prop? Just… Use a unique, special-case prefix that does the same thing as @, with the addition of telling the compiler “This particular decorator is a property decorator within a class constructor”.

Voila!

2reactions
sbuzonascommented, Jun 1, 2017

I’m trying to make a private readonly property not enumerable and end up redefining the property in the constructor to get the value initialized.

export class Character {
    @enumerable(false)
    private readonly attributes: AttributeBag;

    constructor(attributes: AttributeBag = new AttributeBag()) {
        const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), 'attributes'));
        delete descriptor.set;
        delete descriptor.get;
        descriptor.value = attributes;
        Object.defineProperty(this, 'attributes', descriptor);
    }
}

This could be solved by supporting decorators with the shorthand syntax. I disagree that the proposed syntax is confusing. Decorators preceding a parameter name are parameter decorators, and any decorators preceding a token that makes it a property are property decorators.

Another solution could be to support declaration in the constructor even when a property is declared.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP 8: Constructor property promotion - Stitcher.io
In short: property promotion allows you to combine class fields, constructor definition and variable assignments all into one syntax, ...
Read more >
Documentation - Decorators - TypeScript
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use...
Read more >
Typescript Constructor Shorthand - DEV Community ‍ ‍
Here is a thing, in Typescript there is a shorthand to create and assign class properties from constructor params.
Read more >
Understanding JavaScript Decorators - Simple Thread
Decorator is shorthand for “decorator function” (or method). ... Objects in JS have properties, and those properties have values:.
Read more >
Is there a shorthand initializer in Python? - Stack Overflow
If you wish to assign arbitrary values to an instance (i.e. not enforced by the class), you should use a particular data structure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found