Generics & Abstract Classes in TypeScript

Mathew Phillip Wheatley
The Startup
Published in
3 min readOct 9, 2020
Photo by Photo Boards on Unsplash

As I continue the Udemy course TypeScript: The Complete Developers Guide I’ve been introduced to several new to me concepts. Generics & abstract classes are two tools a developer can use to make their code more reusable and maintainable. More senior developers who are accustomed to strong type languages such as C, Go, Java and Swift are likely already familiar with these concepts.

Generics

As discussed in Typescript Type Declaration when declaring functions the input and output types should be annotated but what if you don’t know what type your input or output will be? Initially it may sound like this is a case that never or rarely happens but has you start to abstract it will come up more often than you might expect. Consider the simple function in Figure 1.

Figure 1: Simple pass through function

In the above case the input and output are specified as type any which defeats the whole purpose of TypeScript as it will never find a type error if anything is allowed. Instead we could use generic syntax as denoted by <> to pass in a generic type when the function is called.

Figure 2: Simple pass through function with generics

Now the generic type T is passed as a string when it is called in Figure 2. It is noted that you don’t have to name your generic T , it can be named anything much like a variable. Multiple generics can be passed into a function or class as well, for a function passed generics <T, K> would have access to both of them.

Abstract Class

Now generics seem cool but they really shine when used with abstract classes. An abstract class is a class that itself is never intended to be instantiated, instead they are used to pass properties to sub classes via inheritance.

Figure 3: Abstract Class Car Inherited by Class Sedan

In Figure 3 it can be seen that the class Car is abstract as denoted by the keyword abstract. TypeScript will now throw an error if an instance of this class is instantiated but still allows class Sedan to inherit its properties, in this case the method engineSound via the keyword extends . To use generics with classes you use the same generic syntax as seen previously.

Figure 4: Abstract Classes with Generics

In Figure 4 the class Car now includes a generic T which is used to specify the arguments and return type of method passThrough. You can see that when the sub-class Sedan passes a type of string into the generic therefore requiring the arguments and return types of method passThrough to be string. You can see that when the myCar.passThrough is called with a number an error is raised as denoted by the red underline where as no error is raised when called with a string.

Conclusion

Simple examples were showcased above but you can imagine how generics and abstract classes would allow one to further refactor code for reusability. It is noted that generics are also very useful with using composite classes but composition is beyond the scope of this article.

--

--

Mathew Phillip Wheatley
The Startup

I am a software engineer with a mechanical background. Interests swing from aerospace, to woodworking, travel, skiing, hiking, running, climbing, and lasers.