After discovering a polyfill for Array#includes on Stack Overflow and integrating it into TypeScript, I encountered an issue where adding a small import to my file caused it to be transformed into a module. This prevented me from modifying the global namespace as before.
How can I resolve this polyfill issue?
interface Array<T> {
includes(searchElement: T) : boolean;
}
// Implement Array includes polyfill if necessary
// Link to MDN documentation for reference
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
}