how can you extend native objects in ts.

Topics: General
Oct 16, 2012 at 11:42 AM
Edited Oct 16, 2012 at 11:43 AM

Lets say i want to extend String how can i do this in typescript?

 

interface String { 
	uniqueID(): string;
}

var UID = Date.now();
String.prototype.uniqueID = function() : string { 
	return (UID++).toString(36);
};

String.prototype.uniqueID = function () { 
        // do stuff
}

This will not compile, so what am i doing wrong?

Coordinator
Oct 16, 2012 at 5:07 PM

There's also some guidance on how to do this here: http://stackoverflow.com/questions/12701732/typescript-augmenting-built-in-types.  There is also currently a bug when extending built-ins that we're currently working on fixing.