If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
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.