Transcript from the "Inferencing" Lesson
>> Kyle Simpson: So they can only go so far. They can tell you what we can tell you at compile time, but they can't really tell you anything about what's gonna happen in the real world. They're just gonna hope that none of that bites you.
>> Kyle Simpson: So some examples of TypeScript and Flow, and these examples are actually identical between the two.
[00:00:17] They use identical syntax in these cases. There are a few little nuanced differences. But I was actually remarkably surprised in my research for this preparation how similar TypeScript and Flow are in terms of of what they look like. I expected a lot of differences and there are very few, okay?
[00:00:35] So number one, if you don't do any typing at all, both TypeScript and Flow by default will do some inferencing. So here they're doing a static types inference, which means my intent that they're guessing, is that I want teacher, the variable to only ever hold strings. And when I later try to assign it something non-string, it throws me an error, and says, you're doing an assignment that you shouldn't do.
[00:00:58] That's their best guess. Some people feel that one of the biggest problems within types is that we accidentally assign a value to a variable that is not the type we thought. And so for them, static types are a big deal. I'm being honest with you when I tell you that in my 20 plus years in coding never once, not once has a bug been as a result of me accidentally assigning a number to a variable that used to hold a string, for example.
[00:01:33] I do that on purpose a lot, but never once has that accidentally happened, and been the source of a bug. So for me, that's not one of the problems that I have. If it is a problem that you have, where bugs come because you've reassigned something incorrectly, this is helpful.
[00:01:50] This is one of those features that, for a long time, I said I don't need that part of TypeScript. I don't like for it to complain to me about what I assign variables, cuz I like to be able to assign a number and then later a string. That to me is helpful, so I didn't like this part, and I felt like it was solving a problem I didn't have.
[00:02:24] We can say teacher is definitely a string. We're gonna get basically the same error, but here we're not guessing at the error. We're literally saying I intended for this thing to only ever hold strings, and now you're trying to put something non-string to it. In both cases TypeScript and Flow are gonna throw us an error and say, you're assigning something you shouldn't have, okay?