Skip to content

Instantly share code, notes, and snippets.

@vmadman
Created December 19, 2013 04:13
Show Gist options
  • Save vmadman/8034329 to your computer and use it in GitHub Desktop.
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.
/// <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;"
);
@xps
Copy link

xps commented Dec 19, 2013

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.

@xps
Copy link

xps commented Dec 19, 2013

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