Chuck's Academy

Basic TypeScript

Generics in TypeScript

Generics are one of the most powerful features of TypeScript. They allow you to create components that work with a variety of data types, rather than being limited to a single type. In this chapter, we will explore how generics work in functions, classes, and interfaces to write more flexible and reusable code.

Introduction to Generics

Generics allow a function, class, or interface to work with multiple data types without specifying the exact type beforehand. This makes the code more flexible and reusable.

This image shows an example of GenericsThis image shows an example of Generics

Basic Example of Generics

Let's look at a simple example of a function that uses a generic:

typescript
"Here we define a function called identity that uses a generic parameter T. The function accepts an argument of any type and returns a value of the same type. This is the simplest use of a generic."

When calling the function, TypeScript infers the type of the argument being passed:

typescript
"In this example, we call the identity function with a number as an argument. The compiler infers that the type T is number."

Generics in Functions

Generics are very useful when writing functions that need to handle multiple types. We can also apply constraints to generics to limit the types that can be used.

Example with Constraints

We can constrain the generic type to ensure it meets certain conditions, using the extends keyword.

typescript
"In this example, the getLength function accepts a generic type T that must have a length property. This ensures that the argument passed to the function has a length property, like strings or arrays."

Generics in Classes

Generics can also be applied to classes, allowing a class to work with different data types.

Example of a Generic Class

typescript
"In this example, we define a generic class called Box that accepts a generic type T. The class has a contents property of type T and a getContents method that returns the box's content."

Generics in Interfaces

Interfaces can also be generic, allowing you to define data structures that work with multiple types.

Example of a Generic Interface

typescript
"In this example, we define a generic interface called Pair that accepts two generic types, T and U. Then we create an instance of this interface with a string and a number."

Using Generics with Default Types

It's possible to assign a default value to generic parameters, meaning if no specific type is provided, the default type will be used.

Example of Default Type

typescript
"In this example, the createArray function has a generic parameter T with a default value of string. If no type is specified, the function uses string as the default type."

Conclusion

In this chapter, we have learned about generics in TypeScript and how they allow us to write more flexible and reusable functions, classes, and interfaces. Generics are a powerful tool for working with a variety of types without having to duplicate code.


Ask me anything