Transcript from the "Introducing Type Checking" Lesson
[00:00:21] Right, so you get a value and you try to do something with it. Up until that point we don't care about the types of things. Types exist, if you've ever used type of, you can see like there's some things undefined or a function or a strength. So it's not untyped.
[00:01:05] So if you've used react before and gone through the official tutorials, you will see that proptypes was formerly part of the react library as a whole. It's been refactored out into another library now. But this did what is called run time typechecking. So if you pass things to a component, the types of those things that you passed to the component are verified to make sure that you have alignment with what that component is aligned to receive.
[00:01:32] Facebook also has a project called flow. And if you watch Brian Holt's Complete Introduction to React, version three, I believe this uses flow. And this is static type checking, which is similar to what TypeScript gives us. And TypeScript, of course it's a programming language and a library. That is built and maintained by Microsoft.
[00:01:59] And one of the advantages of static type checking is there is no cost when your program is actually running. This is sort of, it's an advantage in terms of making sure that this eventual product of our programming efforts is fast and it ends up like having no extra weight added to it.
[00:02:20] But it also comes with a disadvantage that we can only do so much with static analysis. Static analysis really doesn't it doesn't tell us the full set of things that could potentially go wrong with our code. Just kinda looking at all of the obvious usages without worrying about all of the particular, like paths that we can take through all the different functions and all the different objects of our code, right?
[00:03:47] There are some that won't, for example, if you had the number five double equals the string with the number five in it, in a strongly-typed world, there's really no way to deal with that kind of comparison, right? In the strongly-typed world, that's apples and oranges. Its strings and numbers.
[00:04:07] So type script relies heavily on type inference which basically starts with some of the most obvious to determined types in your code and expands that information outward in order to figure out larger higher level type information. For example, if you say I have a variable x and that it's initial value is 65, we can say well yes, that has a type of number.
[00:04:37] And if we have a function that eventually returns that, we can say okay, well this function returns a number. We can kind of like expand out relay and use some explicit type information that we can get some you know value literal to you know then determine like what's going on with this function, what's going on with object and you know detect and deduce.
[00:05:02] What various types may be involved in other places. So an important part of this were these type definitions that we've been looking at, .d.ts files where we had the DOM API and we had object.freeze. We looked at that type definition. So this also, whenever you use one of those, that immediately gives TypeScript a clue as to like how this might be used cuz those functions were turn a particular type or they take a certain type of argument and like deductions can be made there.
[00:05:37] One of the core missions of type script is you opt in to a type system here. It is not a situation where you need to like rewrite all of your code. The hope is, one of the happy paths to starting to use type script, is you take you JS files, you rename them to .ts files.
[00:05:59] Files so they're type script and most of your code should work right away. Now you may get a ton of warnings and you may need to supply clues where type inference is not enough to figure out what's going on but it shouldn't be a situation where you must go through and explicitly define as much as possible.
[00:06:45] So if you have an existing project and maybe some subset of your code is particularly complex and type information would go a long way in order to like maintain order in that area. Maintain like that you're doing things in a consistent way. You can up in to, using type analysis in that are of your code without having to bring in the TypeScript compiler to process your files and to like you now have the whole rest of your team learn a new programming language.
[00:07:19] It's just comments, it's just inert stuff that is not technically code, it's just sort of clues around your code. So we're gonna use these JSDoc comments. And Closure Compiler also uses JSDoc comments, so this is another example of like Let's adapt an existing standard, an existing way of doing things.
[00:07:38] Closing compiler, I was using it last 2007, 2008 so that's been about ten years and it still works so let's keep using it. We don't need a new method to do this kind of thing.