Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to always set --alwaysStrict to true in tsconfig.json?

Just curious whether there are any drawbacks to including:

"compilerOptions": {
  "alwaysStrict": true,
 ...
 }

Since it's false by default. Thoughts?

like image 204
Ole Avatar asked May 01 '17 23:05

Ole


People also ask

What is true about Tsconfig JSON file?

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.

What is the purpose of Tsconfig json in Angularjs?

A given Angular workspace contains several TypeScript configuration files. At the root tsconfig. json file specifies the base TypeScript and Angular compiler options that all projects in the workspace inherit.

What is include and exclude in Tsconfig json?

The include and exclude properties take a list of glob-like file patterns. The supported glob wildcards are: * matches zero or more characters (excluding directory separators) ? matches any one character (excluding directory separators)


2 Answers

strict vs alwaysStrict

It is a good idea to not only include alwaysStrict, but to include the strict flag that enables alwaysStrict and also noImplicitAny, noImplicitThis, strictBindCallApply, strictNullChecks, strictFunctionTypes, strictPropertyInitialization and useUnknownInCatchVariables which are even more important.

Confusing names

There is a confusion about names here, because "strict" can mean few things here. The strict TypeScript flag is a shortcut to enable multiple other flags mentioned above. The alwaysStrict TypeScript flag parses your files in parses in the JS strict mode (as opposed to the JS sloppy mode and emits 'use strict' in the output. Instead of using the alwaysStrict flag you could add "use strict"; to all of your files (the ES modules are strict by default).

Other strict flags

The other strict TypeScript flags are even more important, because they help you eliminate these errors:

The strictNullChecks is the most important one in this respect.

It is not enabled by default for backwards compatibility with old code that was written before the new way of checking null and undefined was added.

See this answer for more details:

  • strictNullChecks and null value propagation
like image 132
rsp Avatar answered Sep 27 '22 18:09

rsp


Just curious whether there are any drawbacks to including

Even if your file is not going to be in strict mode, it will be treated like it is. e.g. you will not be allowed to create a function declaration inside a function body : Why TS complains with function declarations inside function body

Personally: Its a good idea to have it on. And an even better idea to always use JavaScript / TypeScript modules (which are in strict mode by default).

like image 31
basarat Avatar answered Sep 27 '22 17:09

basarat