Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;"
);
@vmadman

This comment has been minimized.

Copy link
Owner Author

commented Dec 19, 2013

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

@xps

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.