DEV Community

Cover image for The Art of Reusability: Generics in TypeScript and React
Ibukunoluwa Popoola
Ibukunoluwa Popoola

Posted on

The Art of Reusability: Generics in TypeScript and React

As frontend developers, we're no strangers to the concept of reusability. We strive to write code that's modular, flexible, and easy to maintain. But when it comes to working with different data types, we often find ourselves writing duplicate code or resorting to any types, sacrificing type safety in the process.

In React, we're familiar with the concept of components as functions that take in props and return JSX elements. But what if we want to create a component that can work with different types of data, such as strings, numbers, or custom objects? How can we ensure that our component is type-safe and flexible enough to accommodate different data types?

This is where generics come in – a powerful feature in TypeScript that allows us to write reusable, type-safe code. By using generics, we can create components that can work with multiple data types, without sacrificing type safety or flexibility.

In this post, we'll explore the world of generics in TypeScript, using real-world React components to illustrate and explain. We'll start with a simple example of a generic container component, and then move on to more complex examples, such as a generic list component. By the end of this post, you'll have a solid understanding of how to use generics to write more reusable, maintainable, and type-safe code in your React applications.

What are Generics?
Generics are a way to create reusable functions, classes, and interfaces that can work with multiple data types while maintaining type safety. They allow you to specify a type parameter that can be replaced with a specific type when the generic is instantiated.

Think of generics like a blueprint or a template. You define a generic function or class with a type parameter, and then you can use that generic with different types, just like how you would use a blueprint to build different houses.

A Simple Example: A Generic Container Component
Let's create a simple React component that demonstrates the power of generics. Imagine we want to create a container component that can render any type of data. We can use a generic to achieve this:

interface ContainerProps<T> {
  data: T;
}

const Container = ({ data }: ContainerProps<any>) => {
  return <div>{JSON.stringify(data)}</div>;
};
Enter fullscreen mode Exit fullscreen mode

In this example, we define an interface ContainerProps with a type parameter T. This interface has a single property data of type T. We then create a React component Container that uses this interface as its props type.

Notice the any type passed to the ContainerProps interface. This tells TypeScript that we want to allow any type for the T parameter. However, this can lead to type safety issues, so let's improve this example.

Improving Type Safety with Generics
Instead of using any, we can specify the type parameter when we use the Container component:

interface ContainerProps<T> {
  data: T;
}

const Container = <T,>({ data }: ContainerProps<T>) => {
  return <div>{JSON.stringify(data)}</div>;
};

// Using the Container component with a string type
const StringContainer = () => {
  return <Container<string> data="Hello, World!" />;
};

// Using the Container component with a number type
const NumberContainer = () => {
  return <Container<number> data={42} />;
};
Enter fullscreen mode Exit fullscreen mode

Now, we've improved the type safety of our Container component. When we use the component, we specify the type parameter, which ensures that the data property has the correct type. For instance, for the StringContainer component, if we try passing a number value or a value of any other type e.g. an object, we get an error from Typescript informing us of the type mismatch. Ditto for the NumberContainer component.

A More Complex Example: A Generic List Component

Let's create a more complex React component that demonstrates the power of generics. Imagine creating a list component that can render a list of any type of data. We can use a generic to achieve this:

interface ListProps<T> {
  items: T[];
  renderItem: (item: T) => JSX.Element;
}

const List = <T,>({ items, renderItem }: ListProps<T>) => {
  return (
    <ul>
      {items.map((item, index) => (
        <li key={index}>{renderItem(item)}</li>
      ))}
    </ul>
  );
};

// Using the List component with a string type
const StringList = () => {
  return (
    <List<string>
      items={['Apple', 'Banana', 'Cherry']}
      renderItem={(item) => <span>{item}</span>}
    />
  );
};

// Using the List component with a custom type
interface Person {
  name: string;
  age: number;
}

const PersonList = () => {
  return (
    <List<Person>
      items={[
        { name: 'John', age: 30 },
        { name: 'Jane', age: 25 },
      ]}
      renderItem={(person) => (
        <span>
          {person.name} ({person.age})
        </span>
      )}
    />
  );
};
Enter fullscreen mode Exit fullscreen mode

In this example, we define a List component that takes a type parameter T. The component has two props: items which is an array of type T, and renderItem which is a function that takes an item of type T and returns a JSX element.

We then use the List component with different types: string and a custom Person type. The renderItem function is specific to each type, ensuring that the component is type-safe and flexible.

Conclusion
Generics is a powerful feature in TypeScript that allows you to write reusable, type-safe code. By using real-world React components, we've demonstrated how generics can help you create flexible and maintainable code.

Remember, generics are like blueprints or templates that can be used with different types. By specifying type parameters, you can ensure type safety and flexibility in your code.

I hope this post has triggered your interest in generics in TypeScript and inspired you to write more reusable and maintainable code. Happy coding!

Useful Links

Top comments (0)