This question is a copy of “Use Strict” needed in a TypeScript file?
There are some answers, but it is not clear for what does "use strict" statement in TypeScript, when tsc shoes me strict mode errors without this statement.
But decided to ask as separated question aw well.
I am using TypeScript 1.6 and for me it is not clear what does "use strict" statement add in TypeScript?
Using "use strict"; statement looks like double check. Since tsc shows strict mode errors without this statement.
For example:
class Foo {
03;
constructor(public name:string) {
}
move(meters:number) {
let o = {p: 1, p: 2};
let a;
delete a;
alert(this.name + " moved " + meters + "m.");
}
sum(a:number, a:number, c:number):number {
var sum = 015 +
197 +
142;
var x = 17;
with (obj)
{
x;
}
[1, 2, 3, 4, 5].map(function (n) {
return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
});
delete sum;
return a + b + c;
}
tsc shows me:
- Error:(16, 19) TS2300: Duplicate identifier 'a'.
- Error:(24, 9) TS1101: 'with' statements are not allowed in strict mode.
- Error:(8, 18) TS2300: Duplicate identifier 'p'.
- Error:(2, 5) TS1121: Octal literals are not allowed in strict mode.
- Error:(11, 16) TS1102: 'delete' cannot be called on an identifier in strict mode.
- Error:(16, 9) TS2300: Duplicate identifier 'a'.
- Error:(8, 24) TS1117: An object literal cannot have multiple properties with the same name in strict mode.
- Error:(8, 24) TS2300: Duplicate identifier 'p'.
The
"use strict"
statement affects the runtime, so if something that's invalid in strict mode has gotten past the TypeScript compiler, it will still throw an error at runtime.For TypeScript compile time it adds variable name checks as well. E.g. the following is okay
But the following errors :
Note : TypeScript also prevents other errors irrespective of strict mode declarations which is what is happening in your samples.