Created
December 19, 2013 04:13
-
-
Save vmadman/8034329 to your computer and use it in GitHub Desktop.
This is *my* current temporary fix to get SugarJS's static method additions working in typescript.
This file contains 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
/// <reference path="../path/to/sugar.d.ts" /> | |
/** | |
* Usage Examples: | |
* sgObject.isArray([1]); // -> true | |
*/ | |
declare var sgObject:ObjectStatic; | |
declare var sgNumber:NumberStatic; | |
declare var sgDate:DateStatic; | |
declare var sgRegExp:RegExpStatic; | |
eval( | |
"sgObject = Object;" + | |
"sgNumber = Number;" + | |
"sgDate = Date;" + | |
"sgRegExp = RegExp;" | |
); |
If you want to avoid the eval you could also initialize your objects like this:
var sgObject: ObjectStatic = <any>Object;
...
The only problems I see with this solution are:
- You can't port existing code as-is, you have to replace Object with sgObject
- Methods on native types, such as Date.parse(), won't be accessible via your objects, so you have to use Date or sgDate depending on what method you want to call.
Still, it's better than nothing.
Just realized that regarding the second point, you could actually just copy the definitions over from Date to DateStatic (maybe that's what you did).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm still a bit of TypeScript nub, so maybe its a crap solution, but whatever.. it seems to be working, so far. Its worth noting that my TypeScript files all compile to a single .js file which I'm requiring with NodeJS. This snippet is inserted near the top.
External links on the topic:
Additional Related/Useful Links