Update 2016-03-07: My new library, Doop, is a more practical demonstration of what you can do with decorators.

No great mysteries about what this class does:

class C { foo(n: number) { return n * 2; } }

It’s a stupid example with a method that returns its only parameter multiplied by two. So this prints 46 :

var c = new C(); console.log(c.foo(23));

A feature coming in TS 1.5 that has really caught my eye is decorators. We can decorate the foo method:

class C { @log foo(n: number) { return n * 2; } }

That on its own will not compile, because we need to define log . Here’s my first try:

function log(target: Function, key: string, value: any) { return { value: function (...args: any[]) { var a = args.map(a => JSON.stringify(a)).join(); var result = value.value.apply(this, args); var r = JSON.stringify(result); console.log(`Call: ${key}(${a}) => ${r}`); return result; } }; }

Note: previously I used arrow function syntax to declare value , but as pointed out in this answer on Stack Overflow, that interferes with the value of this , which ought to be passed straight through unchanged.

The three parameters, target , key and value , are passed in by the helper code generated by the TypeScript compiler. In this case:

target will be the prototype of C ,

will be the prototype of , key will be the name of the method ( "foo" ),

will be the name of the method ( ), value will be a property descriptor, which here will have a value property that is the function that doubles its input.

So my log function returns a new property descriptor that the TypeScript compiler will use as the new definition of foo . This means I can wrap the real function in one that logs information to the console, so here I log the arguments and the return value.

So when the resulting program runs in node, it prints two lines instead of just one:

Call: foo(23) => 46 46

This is really quite neat. The next thing I think is: can I decorate a simple value so that it turns into a property that is an observable that can be listened to for change events? I’m going to find out.

Advertisements