Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Inferencing" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle defines what inferencing is in static typing.


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.

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?

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.

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.

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.

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.

But that's what we refer to as static types, inferring that the variable has a type based upon the value that goes into it. JavaScript variables don't have types, but we're layering on this extra requirement if you will, okay? So that's when we don't annotate the types, but of course we can annotate the types.

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?


Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now