Reader small image

You're reading from  Mastering TypeScript - Fourth Edition

Product typeBook
Published inApr 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781800564732
Edition4th Edition
Languages
Right arrow
Author (1)
Nathan Rozentals
Nathan Rozentals
author image
Nathan Rozentals

Nathan Rozentals has been writing commercial software for over 30 years, in C, C++, Java and C#. He picked up TypeScript within a week after its initial release in October 2012 and realized how much TypeScript could help when writing JavaScript. He was one of the first people to start blogging about TypeScript, discussing early frameworks such as Backbone, Marionette, ExtJS and AngularJs. He knew he'd hit the mark when Microsoft staff started to reference his blog posts in their CodePlex discussion forums. Nathan's TypeScript solutions now control User Interfaces in IoT devices, run as stand-alone applications for Point-of-Sale solutions, provide complex application configuration web sites, and are used for mission-critical server APIs.
Read more about Nathan Rozentals

Right arrow

Strict Compiler Options

The TypeScript compiler uses the tsconfig.json file to specify a number of compilation options. These options include what version of JavaScript we would like to generate for, what the output directory should be, and whether or not to allow JavaScript source files within the project directory. We have already discussed a few of these compiler options on our journey with TypeScript so far.

One of the most important options is simply named strict, and this single option turns on a group of other compiler options. The purpose of the strict compiler options is to apply a number of checks to our code and determine whether any of these checks fail. As an example, these strict options can determine if a variable could be undefined at the time of use, or if the variable itself is never used. If we set the value of the strict option to false, or turn it off, then each of these options needs to be turned on individually. In this chapter, we will explore these other...

Nested configuration

The TypeScript compiler is able to re-use a tsconfig.json file in another directory when compiling code in the current directory. This feature is handy if we would like to override a compiler option when running tsc within a specific directory. The tsconfig.json file uses the "extends" option for this purpose. As an example of this nested configuration, consider the following source tree:

├── sub1
│   ├── SampleJsFile.js
│   └── tsconfig.json
├── SampleTsFile.ts
└── tsconfig.json

Here, we have a tsconfig.json file in the project root directory, as well as a TypeScript file named SampleTsFile.ts. We also have a subdirectory named sub1 that contains a tsconfig.json file, and a JavaScript file named SampleJsFile.js. The tsconfig.json file in the project root directory is as follows:

{
    "compilerOptions": {
   ...

Strict Options

In this section of the chapter, we will explore the strict set of compiler options, all of which come into play when we turn the main "strict" option from true to false. Note that if we turn the main "strict" option to false, then we must set the desired "strict" option to true in order for it to become active.

strictNullChecks

The strictNullChecks compiler option is used to find instances in our code where the value of a variable could be null or undefined at the time of usage. This means that when the variable is actually used, if it has not been properly initialized, the compiler will generate an error message. Consider the following code:

let a: number;
let b = a;

Here, we have defined a variable named a that is of type number. We then define a variable named b and assign the value of a to it. This code will generate the following error:

error TS2454: Variable 'a' is used before being assigned

This...

no compiler options

There are also a number of compiler options that are prefixed with the word "no". These options are similar to the "strict" options, in that they further guard our code against things like unused parameters, implicit returns, and implicit any. In this section of the chapter, we will take a look at these compiler options and how they can detect potential errors within our code. These parameters are similar in nature to "strict" parameters, in that they can be turned on or off and can be introduced into a code base gradually.

Note that if the "strict" compiler option has been set to true, then all of these options will be true as well.

noImplicitAny

When a type has not been specified for a property, parameter, or function return type, the TypeScript compiler will automatically assume that it is of type any. In strict mode, this will generate an error. Consider the following code:

declare function testImplicityAny...

Summary

In this chapter, we took a look at the various strict compiler options available within the TypeScript compiler. We started with an example of nested configuration, where a tsconfig.json file can reference another one as a base. We then discussed four strict options that check our code base for possible errors, by detecting null variables, properties that have not been initialized, parameter types when using bind, call, or apply, and strict function types. We then went through the no compiler options, which help us to identify variables that could be of type any, unused variables, implicit returns, switch case errors, and incorrect scoping of this.

It is best practice for any new TypeScript project to leave the default strict option set to true. This will ensure that all of the options we have discussed will always be on and will help to trap a large portion of possible errors within our code. The only time that we should really be modifying these options is if we are...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering TypeScript - Fourth Edition
Published in: Apr 2021Publisher: PacktISBN-13: 9781800564732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Nathan Rozentals

Nathan Rozentals has been writing commercial software for over 30 years, in C, C++, Java and C#. He picked up TypeScript within a week after its initial release in October 2012 and realized how much TypeScript could help when writing JavaScript. He was one of the first people to start blogging about TypeScript, discussing early frameworks such as Backbone, Marionette, ExtJS and AngularJs. He knew he'd hit the mark when Microsoft staff started to reference his blog posts in their CodePlex discussion forums. Nathan's TypeScript solutions now control User Interfaces in IoT devices, run as stand-alone applications for Point-of-Sale solutions, provide complex application configuration web sites, and are used for mission-critical server APIs.
Read more about Nathan Rozentals