1

I am using the Aurelia Cli and it is working great for the most part but 1 thing is really niggling me.

I have created a new prototype extension like so:

interface Array<T> {
   remove(itemToRemove: T): Array<T>;
}

Array.prototype.remove = function (itemToRemove) {
    const index = this.indexOf(itemToRemove);
    if (index !== -1) {
        this.splice(index, 1);
    }
    return this;
}

This works fine except that when i save any file with au run --watch running. I will get an error on build telling me that remove is not a valid method on []. This is random and happens about 1 in 4 times. If i change nothing and save again it will likely not complain again.

How may I get Aurelia to consistently be okay with this?

Link to full project: https://github.com/4imble/StackRpg/tree/master/Client/src

4imble
  • 13,979
  • 15
  • 70
  • 125
  • 1
    I think this sounds like an issue in the TypeScript transpile step, nothing specific to Aurelia. – PW Kad Dec 13 '16 at 19:51
  • Quite possibly I'm not suggesting something is broken in Aurelia or the CLI, just giving some context to my issue. Is there a way to help it along, something like set an ordering so it is looked at first? – 4imble Dec 14 '16 at 00:04
  • Assuming this isn't related - http://stackoverflow.com/questions/12701732/typescript-augmenting-built-in-types - I'm not 100% sure, would need to be more repeatable probably to figure out what may cause it. I'll ping someone with more TS knowledge – PW Kad Dec 14 '16 at 00:11
  • I'm not sure that helps. I'll add a link to my project in case that can give some more context. https://github.com/4imble/StackRpg/tree/master/Client/src – 4imble Dec 14 '16 at 09:10
  • Excellent I'll see if I can pull it down today to reproduce – PW Kad Dec 14 '16 at 14:32
  • I can reproduce but it's 100% TS complaining about extending native arrays, I couldn't get it to work. Perhaps @jdanyow has an idea (he said this worked for him) – PW Kad Dec 14 '16 at 19:17
  • Either upgrading typescript to 2.1.4 or the answer below has managed to fix this for me. Thanks for the help. – 4imble Dec 15 '16 at 13:49

1 Answers1

2

The issue you're seeing is probably the compiler not being fed the array extension source-file consistently. Most likely because the build script is set up to only compile files if they change.

Firstly, try commenting out .pipe(changedInPlace({firstPass: true})); and see if you start getting more consistent builds.

If so, you'll likely have to update the build script such that that source file is always passed through to the compiler. Something like:

let dts = gulp.src(project.transpiler.dtsSource);

let src = gulp.src(project.transpiler.source)
 .pipe(changedInPlace({firstPass: true}));

let exts = gulp.src("path/to/extension.ts")

return eventStream.merge(dts, src, exts)

Also, in typescript 2.1 at least, if I try to set an array prototype as you have, will complain. That can be fixed with:

(<any>Array.prototype).remove = function (...etc
Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
  • This seems to have worked. I cant say for sure however as i can not reproduce it even if i change the file back?! Could be something to do with me upgrading typescript (2.1.4) to try and reproduce the problem that you mentioned. – 4imble Dec 15 '16 at 13:48