I understand that you can not extend the native types like Array and String in TypeScript (TS). I have some small extensions I have written using pure JS, that I would like to expose to TS. For example:
Array.prototype.first = function(testFunction) {
if( typeof(testFunction) !== 'function' ) {
return null;
}
var result = null;
this.forEach(function(item){
if( testFunction(item) ) {
result = item;
return;
}
});
return result;
};
This is in Array.js. How do I expose the 'first' function to TS.
I have tried creating an Extensions.d.ts file which contains:
export declare var Array: {
findItem(callback: Function);
}
And then reference that declaration in my app.ts:
/// <reference path="Extensions.d.ts" />
var x: string[] = new Array();
x.first(function (x) { return false; });
But app.ts doesn't seem to know about the first() function.
Is this possible?
EDIT: Ok, it seems that I need this in my .d.ts file:
interface Array<T> {
first(callback: (Function : T) => boolean) : T;
}
So I guess I just need the following question answered. Consider:
String.format = function () {
var formatString = arguments[0];
if( arguments.length < 2 ) {
return formatString;
}
var args = Array.prototype.slice.call(arguments, 1);
//do formatting here
return result;
}
How do I declare the static extensions ones?