Support property decorators for shorthand constructor-parameter syntax
See original GitHub issueTypeScript 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:
- Created 7 years ago
- Reactions:11
- Comments:14 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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.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.