You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionwrap<T,U>(wrapper: (arg: T)=>U){returnfunction(target: Object,propertyKey: string,descriptor: TypedPropertyDescriptor<(...args: any[])=>T>){varoriginalMethod=descriptor.value;// save a reference to the original method// NOTE: Do not use arrow syntax here. Use a function expression in// order to use the correct value of `this` in this method (see notes below)constnewDescriptor={value: function(...args: any[]){console.log("The method args are: "+JSON.stringify(args));// prevarresult=wrapper(<T>originalMethod.apply(this,args));// run and store the resultconsole.log("The return value is: "+result);// postreturnresult;// return the result of the original method}};returnnewDescriptor;}}functionlen(val: string): number{returnval.length;}classMyClass{
@wrap<string,number>(len)myMethod(arg: string){return"Message -- "+arg;}}constt: number=newMyClass().myMethod("testing");
Expected behavior:
I expect this to compile, and modify the return type of MyClass#myMethod.
Actual behavior:
tsc decorators-broken.ts --experimentalDecorators --target es5
decorators-broken.ts(25,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
Type '{ value: (...args: any[]) => number; }' is not assignable to type 'void'.
decorators-broken.ts(31,7): error TS2322: Type 'string' is not assignable to type 'number'.
I'm trying to create a decorator that wraps methods, potentially altering the wrapped method's return type, making function composition possible through decoration. I initially thought this was a bug, but upon inspecting MethodDecorator's type signature, this is probably intentional:
This one is probably more difficult to add, and I know mixins are already on the TS roadmap, but if the type information of a class could be altered by decorators, that would be quite nice:
import { mixin } from 'core-decorators';
const SingerMixin = {
sing(sound: string) {
alert(sound);
}
};
const FlyMixin = {
// All types of property descriptors are supported
get speed(): number { return 42; },
fly() {},
land() {}
};
@mixin(SingerMixin, FlyMixin)
class Bird {
singMatingCall() {
//TODO: For @mixin, I don't know how we can make it work for TypeScript...
// this.sing('tweet tweet');
}
}
var bird = new Bird();
bird.singMatingCall();
bird.fly();
tsc core-dec-test-2.ts --experimentalDecorators --target es5
core-dec-test-2.ts(28,6): error TS2339: Property 'fly' does not exist on type 'Bird'.
TypeScript Version:
nightly (1.9.0-dev.20160217)
Code
Expected behavior:
I expect this to compile, and modify the return type of
MyClass#myMethod
.Actual behavior:
I'm trying to create a decorator that wraps methods, potentially altering the wrapped method's return type, making function composition possible through decoration. I initially thought this was a bug, but upon inspecting
MethodDecorator
's type signature, this is probably intentional:Can we add something like the following?
The text was updated successfully, but these errors were encountered: