javascript - Can't use new ecmascript decorators in typescript 2.4.2 -


here code example:

function enumerable(value: boolean) {   return function (target: any, propertykey: string, descriptor: propertydescriptor) {     descriptor.enumerable = value;   }; }  class {   @enumerable(false)   a: number = 1   b: number = 2    mymethod () {} }  const = new a() 

whatever try get:

d:(real path removed)/first-try-typescript>tsc --emitdecoratormetadata --experimentaldecorators decorators.ts decorators.ts(8,3): error ts1240: unable resolve signature of property decorator when called expression. 

i have tried same stackoferflow questions suggestions:

  • adding emitdecoratormetadata & experimentaldecorators tsconfig
  • running tsc --emitdecoratormetadata --experimentaldecorators
  • adding :any mark decorator function returning value
  • adding descriptor: typedpropertydescriptor<any> type

i error. both in terminal , in webstorm code hints. method decorator - same thing (see example below).

function test(target: object,               propertykey: string,               descriptor: typedpropertydescriptor<any>): {   return descriptor; }  class {   a: number = 1   b: number = 2    @test   mymethod () {} }  const = new a() 

up date code here - https://github.com/rantiev/first-try-typescript

unfortunately, property decorators not have access property descriptor, properties live on class instance, while decorators evaluated before instance possibly exist. also, can use following signature property decorator:

function (target: any, propkey: string | symbol) 

so no descriptor here.

you can't object.defineproperty(target, propkey.tostring, { enumerable: false, value: ... }) because shared across instances of class, i.e. setting property in 1 instance leak another.

achieving doing possible though, bit complicated. create getter on prototype creates desired property descriptor in time. like:

function enumerable(value: boolean) {     return function (target: any, propkey: string | symbol) {         object.defineproperty(target, propkey, {             get: function () {                 // in here, 'this' refer class instance.                 object.defineproperty(this, propkey.tostring(), {                     value: undefined,                     enumerable: value                 });             },             set: function (setvalue: any) {                 // in here, 'this' refer class instance.                 object.defineproperty(this, propkey.tostring(), {                     value: setvalue,                     enumerable: value                 });             }         });     }; } 

the "outer" get/set functionality run once, instance property shadow after property descriptor has been created on instance.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -