Check out a free preview of the full TypeScript Fundamentals course:
The "Challenge 5: Typed Stack" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

In this challenge, students build a stack data structure using generics. Mike takes questions from students.

Get Unlimited Access Now

Transcript from the "Challenge 5: Typed Stack" Lesson

[00:00:00]
>> Mike North: So we're gonna create a typed stack, if you don't know what a stack is, it is a data structure where you push a bunch of stuff on and then you peel them off almost like a stack of paper, so its the first on is the last off, first on, last off.

[00:00:19] So you're gonna build those Stack data structure that uses generics to constrain the types that it accepts. Overload the push functions, so it can take either a single item or an array of items, so you'll probably have a type of fifth type of array. Iterator of those things and push each one of them in a sequence.

[00:00:40] So as you can see here, I can push this array, cherry apple grape and then push a lemon. And then, when I pull them off, one by one, it's clear that the array was not one thing pushed in. And for that matter, like I'm allowed to push in an array of strings like that, despite the fact that stacks type is string.

[00:00:58] Normally that would be a type error, right, if it were trying to use the individual push, this is another case where type safety is gonna help us really express our intent very well here. We can take both of them, but it's very clear what one means or what the other means.

[00:01:13] Pop should return the object of the appropriate type, keep the internal data structure private, using your access modifier keywords and my advice here if you want to implement things the way I implemented this just for fun, just for extra practice with types, I implemented this with a linked list.

[00:01:33] Where you basically hold onto the head and you chain these things together, I did this because there's no data structure in JavaScript, that really lends itself well to that kind of thing. So it forces you to actually work with types and have a type and an interface for your node for your linked list and make that work.

[00:01:52] Here is the stack interface it should be already in the starting point for your exercise. So to pass the test you basically must satisfy this interface here. Which you can see is expressed in terms of a generic. The I in front of stack, this is a convention that I'm teaching you now.

[00:02:15] This is what, like large scale type script applications typically do. It lets you know when you grab, when you see that I something in your code, you know it's an interface, I for interface. Whereas if we just saw a stack, you don't really know if that's class or what.

[00:02:32] So this makes it extremely easy to know what is just an expression, a description of structure versus a constructor. So if you like TS length which is the common, it is the type script variant of ES length think of it that way, it is a static code analysis tool.

[00:02:49] The default set of rules will have you like interfaces always begin with I. So starting now, I'm going to ask that you like start working with that convention.
>> Speaker 2: And do they follow the, all generics or t, or t something as well?
>> Mike North: It's usually single capital letters starting with t.

[00:03:09] So your first word would be t if you takes two it would be t and u and so on. That being said you can do whatever you want. But I developed those habits with C++ and so I do it that way too. T-U-V or S and T, it's usually stuff around T.

[00:03:26] It's not very important but that's what you see a lot [LAUGH]. You can do whatever you want. But like here, if you show this to a type script person, they'll say like " hu type parameter, absolutely, It is definitely what that is.