Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Introduction to Generics in Typescript


    If Typescript is the first language in which you've encountered generics, the concept can be quite difficult to understand. We skip the lecture in this lesson and dive straight into a real-world use-case that is guaranteed to help you understand the need for generics.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Here we have a simple emitter class. It has a single emit method that takes an event and logs it to the console. Then we create a new instance, and we call it Emit Method. In this case, we pass it to an object literal that has the path and directory properties.

    Note, that at this point, this is all just plain JavaScript, so if inside our application we're expecting events to be in exactly this pattern, but you provide a typo, you're not going to get any warning about that here.

    To fix that, we can use an interface, and we'll call this "My Event." We'll say that path needs to be a string and directory is a Boolean. To use this interface, you could actually put it here. Now any typos will show up in the editor and also in the TypeScript compiler.

    That's not very useful, and having these right next to these object literals in the code can be a bit gross. You can move it to instead be on the function signature. Here you can say, "Emit takes a single argument."

    Here, we use the local name "event," and it must conform to the interface "My Event," which requires these properties. So far, so good. Now we have clean code, and any typos are picked up by the compiler, but we have a problem here.

    Let's say we want to reuse this class to create another emitter that is doing something different in our app. When it calls the emit method, we want to pass a type. We're going to pass it through a string which will be something like Start, and that's it.

    Now you can see we have this problem, because we hard coded the My Event interface into the class definition, and now this no longer conforms. How do we get around this? First of all, let's create the second interface.

    This one's a bit simpler. It just has type. We'll call this "My Event 2." How do we sometimes use one interface for one emitter, and sometimes use another interface for another emitter.

    Just for completeness, I'll show you that you could actually use the pipe here and say that it's "My Event," or "My Event 2," but you can imagine that this is not scalable either, and you're still coupling these interfaces into this class.

    We need something better, and this is exactly where generics can help us. You may have seen in some examples or in documentation the use of this syntax. "T" doesn't actually mean anything here. It could be a lowercase t or it could be the word car or anything you want, but T is the convention to use, T for type.

    All we can do is remove these from there and provide T. Now we have a generic class that has the type of T associated with it, and then we can reuse that type throughout any of the method signatures.

    Now onto the next problem. All of the sudden, we've lost all our type checking. Because we've said this is generic, at this point, it's allowing anything through. What we do is we provide the interface here when we instantiate the object.

    This first one will be My Event, and this one will be My Event 2. Note that we now get the red squiggly, as we have a type here. Correct that, and then call Emitter 2 instead, and you can see all ones have gone away.

    Now we have two separate interfaces, but we have a generic class, and this allows us to have type checking across multiple instances, all with different interfaces.