1

In extensions.js, I have the following code:

Array.prototype.shuffle = function() {
    for (let i = this.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [this[i], this[j]] = [this[j], this[i]];
    }
}

In test.js, I import the file, and then try to use the new prototype function:

import 'extensions.js';

function() {
    var arr = []; // or new Array()
    arr.shuffle();
}

Unfortunately, the import doesn't seem to augment the Array type.

[ts] Property 'shuffle' does not exist on type 'any[]'. any

John Difool
  • 5,572
  • 5
  • 45
  • 80
  • This is the correct answer, that solves your problem: https://stackoverflow.com/a/12803141/1679286 – winner_joiner May 25 '18 at 19:20
  • @winner_joiner: I tried that earlier, by adding Interface in my ts file but it still doesn't get recognized by the compiler. The difference is that I need this extensions (think more than a few lines) in multiple files. So copy/pasting would be frugly. – John Difool May 25 '18 at 19:25
  • 3
    You should add typing file so your compiler recognize the new added function. – Nour May 25 '18 at 20:39

0 Answers0