ptriller ptriller - 3 months ago 31
TypeScript Question

Typescript PropertyDecorator do not work as advertised

When I try to code a simple Property Decorator in typescript the PropertyDescriptor is always null. If I use the following code and compile it with:

node_modules/.bin/tsc -experimentalDecorators --target es5 test.ts

then the PropertyDescriptor of "somestate" is always null.

function test():any {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor):void {
console.log("Descriptor %o", descriptor);

export class TestClass {

public somestate: string;

new TestClass();

This behaviour does not change no matter what I chose as target.

The generated decoration call is actually, and that obviously will always be bot set.

], TestClass.prototype, "somestate", void 0);


AFAIU, this is the expected behaviour.

According to the TypeScript documentation, Property Decorators section:

NOTE  A Property Descriptor is not provided as an argument to a property decorator due to how property decorators are initialized in TypeScript. This is because there is currently no mechanism to describe an instance property when defining members of a prototype, and no way to observe or modify the initializer for a property. As such, a property decorator can only be used to observe that a property of a specific name has been declared for a class.