ChatGPT解决这个技术问题 Extra ChatGPT

"Use Strict" needed in a TypeScript file?

I've seen posts regarding where to put the "use strict" line in a TypeScript code file. My question is, why have it at all?

Since TypeScript is already a strongly typed language, what does "use strict" add?

The link you provided defines what "use strict" is for in a JavaScript file. My question is whether or not it is still useful/needed in a TypeScript file where TypeScript and Typescript-enabled editors will catch most/all(?) of the issues that "use strict" would catch.
You would also need it if you are building it to ES6 then using something like Babel, it requires the generated script to contain "use strict";
Don't put it in TS filed, add the flag in tsconfig.json --alwaysStrict:Parse in strict mode and emit "use strict" for each source file
use strict relates to ECMAScript 5 strict mode. I wrote an article about Typescript strict type.Check here for a detailed explanation

D
David Sherret

Updates

TypeScript 1.8+: "use strict"; is emitted in modules (Read more).

TypeScript 2.1+: --alwaysStrict compiler option parses all files in strict mode and emits "use strict" at the top of all outputted files (Read more).

You can find a list of some examples by searching TypeScript's tests for "in strict mode".

Here's some examples of code that will only throw a compile time error when you "use strict";:

// future reserved keyword not allowed as variable name
var let,
    yield,
    public,
    private,
    protected,
    static,
    implements;

// "delete" cannot be called on an identifier
var a;
delete a;

// octal literals not allowed
03;

There are a few more examples where "use strict"; would throw an error only at runtime. For example:

"use strict";
delete Object.prototype;

Personally, I don't find it all that useful at preventing me from making mistakes in TypeScript and the additional noise it adds to a file makes me not bother writing it. That said, starting in TS 2.1 I'll enable the --alwaysStrict compiler option because it adds the slight additional strictness without any code maintenance overhead.


Cool that the TypeScript unit tests cover this. I'll check it out. Thanks for the examples. And I agree about no longer including it in our "coding standards" for TypeScript.
Note that if you use external modules, any use strict string declared at the top of a TS file may appear insde the module function instead of at the top of the file, thus slightly alter the semantics.
on my machine, TypeScript 1.4 does not allow octal literals when targeting ECMAScript 5 or higher, even when not using "use strict;"
O
Oleg Valter is with Ukraine

For my money, yes, "use strict"; should be included in TypeScript files.

Disregarding the compile time effects of "use strict"; on TypeScript, there is likely a runtime impact when the generated JavaScript is executed:

MDN identifies performance improvements in avoiding boxing this in function calls, and the removal of the function.caller and function.arguments properties.

Jeff Walden of Mozilla has also hinted at opportunities for performance gains in this answer.


Your points for using ECMAScript strict mode are valid, but as pointed out in the accepted answer, it is better to achieve this by enabling the "alwaysStrict" TypeScript compiler option rather than including it manually in your .ts files.
@bgh I agree. Do note though, the question and answer predates --alwaysStrict (introduced in version 2.1). So when the question was asked, there was no distinction between "should "use strict" be in the TS source", and "should "use strict" be in the emitted output."
I think the "update: use --alwaysStrict" note on the accepted answer is sufficient, but if you feel differently please do edit this one to add something similar.