Skip to content

Instantly share code, notes, and snippets.

@diverted247
Last active August 29, 2015 14:19
Show Gist options
  • Save diverted247/a885c3aaea4d71c79534 to your computer and use it in GitHub Desktop.
Save diverted247/a885c3aaea4d71c79534 to your computer and use it in GitHub Desktop.
TypeScript 1.5.0-alpha Decorators
mkdir testDecorators
mkdir testDecorators
npm init
npm install [email protected] --save
node_modules/typescript/bin/tsc decorate.ts
node decorate.js
false -> true
true -> false
var __decorate = this.__decorate || function (decorators, target, key, value) {
var kind = typeof (arguments.length == 2 ? value = target : value);
for (var i = decorators.length - 1; i >= 0; --i) {
var decorator = decorators[i];
switch (kind) {
case "function": value = decorator(value) || value; break;
case "number": decorator(target, key, value); break;
case "undefined": decorator(target, key); break;
case "object": value = decorator(target, key, value) || value; break;
}
}
return value;
};
var Debug = (function () {
function Debug() {
}
Debug.assert = function (isTrue) {
return isTrue;
};
Object.defineProperty(Debug, "assert", __decorate([flip], Debug, "assert", Object.getOwnPropertyDescriptor(Debug, "assert")));
return Debug;
})();
Debug.assert(false);
Debug.assert(true);
function flip(target, key, value) {
var _this = this;
return {
value: function (isTrue) {
var result = value.value.apply(_this, [isTrue]);
console.log(isTrue + " -> " + !result);
return (!result);
}
};
}
class Debug {
@flip
static assert( isTrue:boolean ):boolean {
return isTrue
}
}
Debug.assert( false );
Debug.assert( true );
function flip(target: Function, key: string, value: any) {
return {
value: (isTrue: boolean) => {
var result = value.value.apply(this, [isTrue]);
console.log(isTrue + " -> " + !result)
return (!result);
}
}
}
@nycdotnet
Copy link

This is cool. Quick fixes: requires -t es5; the files are decorators.* but the instructions say decorate.ts; and I think you meant cd testDecorators on the second line. Thanks, Ted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment