Visual Studio Code

Categories of Type Systems

Visual Studio Code

Check out a free preview of the full Visual Studio Code course

The "Categories of Type Systems" Lesson is part of the full, Visual Studio Code course featured in this preview video. Here's what you'd learn in this lesson:

After discussing the difference between nominal and structural type systems, Mike states that VS Code uses structural type system.


Transcript from the "Categories of Type Systems" Lesson

>> Mike North: Hopefully I've convinced you of the importance of types. It is of benefit to you to write your code in a way that is consistent and aligned with a strongly typed language. So there are two, there are many ways to categorize type systems. I'm just gonna worry about one for now.

One type systems are either nominal or structural. Nominally typed or nominal typing means literally just based on name only. So if you say, I'm a function and I take as an argument, I take like a cart item. Say we have a representation of an item that goes into our shopping cart.

So I'm function I take a cart item. You must give me a cart item, or I'm going to say there's been a type mismatch. In structural typing, we just care about the properties, and the types of those properties on an object and when we say like I take an object of type cart item, what we're really describing is, look I have a particular structure of an object and I can refer to it as cart item.

Like there's a name for this structure but as long as you match this structure and you can have extra properties in there that's fine, or you could just create a new object literal and pass it in, as long as the structure of those objects align, we're good, we're fine.

So here's, this is typescript here, just to, but hopefully it is reasonably self-explanatory. If we have a CartItem, we can say we have an item, that's an object that has these three properties with those types and then it has a quantity. So all we care about is the structure of an object.

And if the structure is good enough, right. If you have this, at least, we would say, okay, well now. Like I can say, you're equivalent to the type that I need. Right. You could have extra stuff in there, maybe if we added like a serial number to it.

That would be fine because it'll just kinda be ignored along the way. So, this is a, this is really one of the keys that allows us to use type inference and to not have to put annotations all over the place, because as long as you take something that meets the minimum requirements, we don't care what it's called.

We just care about the structure and the benefit here is, if you say I'm a function and I take an object of this type, now you can have a higher level of assurance that what you are given is gonna have, you know, a quantity on it. You don't have to do any kind of defensive programming.

Because you'll get some assurance based on what can be done with static analysis, that whatever is calling this should be passing something in. Now it's not bulletproof, you of course can trick it but like it is better than regular JavaScript in this respect.

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