Last active
August 29, 2015 14:19
-
-
Save diverted247/a885c3aaea4d71c79534 to your computer and use it in GitHub Desktop.
TypeScript 1.5.0-alpha Decorators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is cool. Quick fixes: requires
-t es5
; the files aredecorators.*
but the instructions saydecorate.ts
; and I think you meantcd testDecorators
on the second line. Thanks, Ted!