Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "use strict" add for TypeScript code?

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'.
like image 529
Yauhen Avatar asked Nov 09 '15 13:11

Yauhen


People also ask

What is strict mode in TypeScript?

TypeScript is configured by using a tsconfig. json file. Enabling strict mode is as simple as doing: And what this does is enable noImplicitAny , noImplicitThis , alwaysStrict, strictBindCallApply , strictNullChecks , strictFunctionTypes , and strictPropertyInitialization under the hood.

What is the point of use strict?

Strict mode makes several changes to normal JavaScript semantics: eliminates some JavaScript silent errors by changing them to throw errors. fixes mistakes that make it difficult for JavaScript engines to perform optimizations.

What is the benefit of using use strict `?

First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.

Why do we use strict mode in JavaScript?

Strict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.


1 Answers

I am using TypeScript 1.6 and for me it is not clear what does "use strict" statement add in TypeScript?

For TypeScript compile time it adds variable name checks as well. E.g. the following is okay

var implements = 123;

But the following errors :

"use strict";
var implements = 123; // Error: implements is a reserved keyword in strict mode 

Note : TypeScript also prevents other errors irrespective of strict mode declarations which is what is happening in your samples.

like image 163
basarat Avatar answered Nov 10 '22 14:11

basarat