Strict Compiler Option

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional TypeScript Masterclass Lessons

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11

Strict Compiler Option

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Without strict

By default no errors in this code:

function add(first, second) {
return second + first;
}

add(1, 2); // 3
add('Hello', 3); // 😬

Adding strict

Add to your tsconfig.json:

{
"compilerOptions": {
"strict": true
}
}

With strict

You are forced to add annotations that will catch errors:

function add(first: number, second: number) {
return second + first;
}

add(1, 2); // 3
add('Hello', 3); // Error!
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

When you use the types strip compiler to initialize a new types strip configuration using TSC minus minus in it, the generated ts config js has this option called strict, which is by default true strict option is actually a collection of options and if you set strict to true, all of these options are implicitly set to true. If you set it to false, all of these options become false. Now you can still override the individual options. For example, you can set strict to true

00:33

and set no implicit any to false and that will mean that all of the other options are still true, but no implicit any is false. Now let's look at the impact of setting strict to true or false with some examples. On the left we have a TypeScript file from a project with strict set to false and on the right we have a TypeScript file from a project with strict set to two. Here we have a function that is designed to add to numbers. Now for numbers, this function is perfectly fine and indeed when you evoke it with something like one

01:07

and two, you get the expected result of three. However, notice that we haven't provided any type annotations for the parameters first and second. What this means is that TypeScript will infer them to be of type any allowing you to invoke the function with all types, for example, with two strings. And you might think that it is perfectly fine to do so in this case. However, the result might be surprising. You probably expected Hello world and instead you got Word hello. This is because the developer of the ad function expected you to call it with numbers

01:40

and the order of parameters. It doesn't matter in that case. So let's go ahead and copy all of this code from Strict Fault to a project. With Strict True, we immediately get two errors from TypeScript with strict True. If you fail to provide type annotations for something that TypeScript cannot infer, it raises a compile time error. As an example, the error for the first parameter is that parameter first implicitly has an any type. Now the fix for this particular error is to add a type annotation, and in our case, the proper type annotation is

02:13

the annotation phone number. And with these annotations in place, any invalid usages, for example, an invocation with strings will result in a compile time error and we can go ahead and delete those. Now of course, with strict mode, you are still allowed to explicitly add an any annotation. For example, here we have a log function that can in reality work with any type, so we can get rid of the error on the value parameter by adding an explicit type annotation of any now error on an inferred any is just one of the features that are provided by the strict compiler option.

02:48

Let's look at an example of a different feature. Here We have a class for a point in two dimensional space with numbers X and Y of type number and a method move that allows you to shift the the values of X and Y. We can create an instance of this class and then provide values for the X and Y properties and then use the move method to move these X and Y by some amount and log out the results. Now at this point, all of this code is perfectly fine. However, it is not great API design. We implicitly expect that the creator of the point has provided values for the X and Y members.

03:23

It is perfectly possible that we might end up with the point created where X and Y have not been provided. In this case, the move function will have varied results. That is it'll set X and Y to not number. Now, let's go ahead and copy this code over to our project. With Stretch Drew, we immediately get two compile time errors. The errors are that the properties are not initialized and are not definitely assigned in the constructor. What this means is that you might end up with a point with the X and Y members are not actually numbers. Now, one of the ways in which we can fix this error is

03:58

by ensuring that we actually initialize X and Y in the constructor by taking the initial values as parameters. And now if anybody wants to create a point, they will have to provide initial values ensuring that the X and Y members are always numbers for all instances of our points. Now, these definite assignment checks are just another feature that is a part of the straight suite. Let's look at another example of a different feature. Here we have a type representing a user in a database with the properties name and age, and then we have an in-memory database

04:30

of users with two members. Now, if you want to get the age of a particular user given the username, we can create a utility function which accepts the name as a parameter, goes through the list of users to find the user with the user name, matches the input name, and then finally returns the found user's age. Now this code looks perfectly fine and indeed it does function if the user is found. Now let's go ahead and copy this code over into our project with strict True,

05:02

and as soon as we do that, you can see that TypeScript has found a potential error. The error in this case is that we are trying to access the age property off of a user that may or may not be found. Now this will result in an ugly runtime error like cannot read age of Undefined. Now because TypeScript has caught this error at compile time, we can raise a nicer error like user not found, which is the actual source of the issue. And now thanks to Typescripts code flow analysis, it knows that usable monogamy undefined and allows you to access the age property.

05:36

Now once more, this is just one of the features that is provided by strict. Now let's jump back and discuss whether you should set strict to true or false. Enabling this option is a programming preference if you prefer dynamic typing overall, but want as much of the free benefits you can get from TypeScript set strict to false. But if like me and many of the TypeScript developers, you want as much type safety as possible, you should leave strict as true, which is the default option. One final consideration worth mentioning is

06:09

that if you are working in an education setting, you might want strict as off to provide a more gradual introduction to TypeScript.

Professional TypeScript Masterclass

Professional TypeScript Masterclass

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11