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

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

Mike shows how to define custom named types using the @typedef tag


Transcript from the "Custom Types" Lesson

>> Mike North: So, this is great. And what I wanna have you do, is we're gonna define our own name to types just using comments. The way you do that is with this typedef, comment type. So, what we're effectively saying here is we have a type, we're going to refer to it as InventoryItem.

Now, whenever we're using that @type thing in a comment, we can say well this is an InventoryItem. And we're basically saying I'm going to refer to a structure by name. And with this typedef, this is where we define what that structure looks like. And you can see here it's just an object that has a property called, name, whose type is a string and a property called price, whose type is a number.

We can combine that with another type and we can call this an invoice item. And you see here, rather than just saying object, we're actually in lining another type, it's a different way. So, this object here, that's just another type definition. Sorry, this is just another type definition.

We could put whatever we want there, including something that looks like this, which is not just an object, but it's an object that it's of a particular shape. And so we're saying okay, we've got an InvoiceItem which says okay, item whose value is an InventoryItem, that's the thing you took off the shelf and here's how many you took.

And then down here we could define, we could say okay well invoice simply an array of invoice items. And we could push an item into that and it would be happy. And of course we try to push something into it with a different shape, this is square peg round hole.

It won't work.

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