Description
As per #4270, the supported way to control strict mode when not using ES6 modules is to put "use strict";
at the top of the file. However, when the --outFile
option is used to concatenate the emitted code for the input files into a single output file, the strict mode of the output file ends up being controlled by the first input file; attempting to enable strict mode on some input files and not others does not work. The compiler could warn if a user tries this by mistake.
TypeScript Version: 2.0.3
Code
// a.ts
"use strict";
var a = 1;
// b.ts
var b = 2;
tsc a.ts b.ts --outFile ab.js
Expected behavior: Semantic error: "Cannot enable strict mode on a subset of input files when using --outFile" or similar.
Actual behavior: No warning and the following output:
"use strict";
var a = 1;
var b = 2;
So strict mode is enabled on both input files, or neither if the order is reversed.
Activity
RyanCavanaugh commentedon Oct 10, 2016
I think #10758 would handle your use case? It's likely some people want to only have to write "use strict" at the top file in concatenated output, so I wouldn't want to break them.
mattmccutchen commentedon Oct 10, 2016
Indeed. Thanks for the reference; I'm not having much success finding these existing issues myself. :(
If a user does that, then all of the other files will use strict mode for the eventual parsing and execution by the JavaScript engine, but not for the parsing by the TypeScript compiler. Do you think that's a reasonable thing for a user to want? I don't think the inconsistency is reasonable.
mattmccutchen commentedon Dec 5, 2016
I believe that while this is still a valid suggestion in principle, it almost certainly won't be worth implementing now that
--alwaysStrict
is implemented (note, not yet released), so closing. Comment if you disagree.