Title: grunt-ts watch not working when file changed
Tags: typescript;grunt-ts
Question: I am using grunt:
"dev-build": {
src: ["src/**/*.ts", "typings/vendors.d.ts","typings/tsd.d.ts", "!libs/**/*.ts"],
outDir: "artifacts/dev",
watch: "src/**/*",
options: {
// comments: true,
//      baseDir: 'src/',
module: "amd",
target: "es5",
sourceMap: true,
declaration: true, //inlineSourceMap :true,
//emitDecoratorMetadata:true,
//experimentalDecorators:true,
}
}
and everything compiles fine:
"TypeScript compilation complete: 8.97s for 256 TypeScript files."
but then after changing a file (adding a linebreak) without changing the code the compile breaks with the watch:
### changed  &gt;&gt;src/FxsPortal/FxsBaseItemViewModel.ts
Compiling...
### Fast Compile &gt;&gt;src/FxsPortal/FxsBaseItemViewModel.ts
Using tsc v1.6.2
C:/dev/AscendXYZ Portal/src/FxsPortal/FxsBaseItemViewModel.ts(2,21): error TS2307: Cannot find module 'knockout'.
I know there are no errors since I can compile it all manually. I dont seem to understand why it do not work when the watch of ts-grunt triggers.

Here is the accepted answer: For reference I just wanted to share how I solved this using an external watch.
watch: {
devBuildWatch: {
files: ['src/**/*'],
tasks: ['ts:devBuild'],
options: {
spawn: false,
},
}
},
and a onchange action
var changedFiles = {};
var onChange = grunt.util._.debounce(function () {
grunt.config('ts.devBuild.src', Object.keys(changedFiles).concat([ "typings/vendors.d.ts","typings/tsd.d.ts", "!libs/**/*.ts"]));
changedFiles = Object.create(null);
}, 50);
grunt.event.on('watch', function (action, filepath) {
changedFiles[filepath] = action;
onChange();
});
Comment for this answer: if you have a watch running, then grunt.event.on("watch" is firing on changes
Comment for this answer: Thanks - How is the "onchange" code linked to the watch task?

Here is another answer: I dont seem to understand why it do not work when the watch of ts-grunt triggers.
Those errors can be safely ignored. The reason why they show up is because grunt is just driving tsc from the command line and only passing it individual files.
If you really care you can use reference tags to bring in the global .d.ts files (files that are not coming in via import/require). But I highly recommend against it. You should have an IDE open that will give you the real errors anways (...cough atom-ts ...)
Comment for this answer: Hi @basarat, I also encountered this issue. Is there a new solution for that? Ignoring the console is not a great one, kinda misses the purpose of watch IMO.
Comment for this answer: @basarat - the real issue here is that, true, I can ignore the errors and the file will compiled but since it's an error, when I'm undoing the latest code change I get: `No file changes were detected. Skipping Compile`- so I'm staying with the compiled code that produced the error.
Comment for this answer: Thanks! I will look into atom-ts. currently I am using the VS typescript project side by side to give highlight.  I understood the documentation such that reference was a outputfile? Is this wrong, so I can put my tsd.d.ts into reference? (can it take an array also)?
Comment for this answer: sorry, you ment reference in the ts files. not the option in ts-grunt
Comment for this answer: take a look at my answer for external watch.  If we had an option on ts-grunt such the internal watch did similar of concatting a list of type definitions.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/12/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<typescript><grunt-ts>', 'Title': 'grunt-ts watch not working when file changed', 'ViewCount': 1103, 'id': 33080502, 'user_AboutMe': '<p><a href="https://www.linkedin.com/in/pksorensen/" rel="nofollow noreferrer">https://www.linkedin.com/in/pksorensen/</a></p>\n\n<p>I can provide you with Azure, D365 and Sharepoint consultants. I work myself with Azure :)</p>\n', 'user_AccountId': 466930.0, 'user_DisplayName': 'Poul K. Sørensen', 'user_Reputation': 16394.0, 'user_Views': 1961.0, 'user_id': 871985.0}}