Generics in Typescript – How To Type Reusable Parts of Code?
Tags: react, typescript, webdev
Published: 2020-03-30 08:47:40

<p>If you follow the DRY rule in writing your code in Typescript, the Generic Types are for you! </p>
<p>Thanks to Generic Types you can easily create reusable functions, interfaces or classes instead of writing single types for each one. </p>
<p>Generics allow you to use different types of providing parameters to one reusable part of code.</p>
<p>The <code>&lt;&gt;</code> syntax is reserved for describing generic type. Between the <code>&lt;&gt;</code> you can provide type which can also take default or static values.</p>
```javascript
interface User<T=string> {
  nick: T
}
 
const user1:User = { nick: 'User1'}
const user2:User<'User2'> = { nick: "User2"}
```
<h2><b>Let’s get some practice!</b></h2>
<p>We would like to create a simple React App with TS which displays 3 titles of fetched posts with author name, loader if data is fetching and the error if we couldn't get data somehow.</p>
<p>To understand how it exactly works, let’s start with creating two custom hooks - one for fetching user data and the second one to get post data.</p>
```typescript
// First hook

interface User {
  id: number;
  username: string;
  email: string;
  name: string;
}
 
// Interface which describes data returned by useUsersFetch hook 
interface ReturnedUserData {
 data?: User[];
 error?: string;
 loading: boolean;
}
 
export const useUsersFetch = (): ReturnedUserData => {
 const [fetchedData, setFetchedData] = React.useState<
   ReturnedUserData>({
     loading: true
 });
 
 React.useEffect(() => {
   const fetchData = async (): Promise<void> => {
     try {
       const { data } = await axios.get(`${base}/users`);
	
       setFetchedData({ data, loading: false, error: undefined });
     } catch {
       setFetchedData({
         data: undefined,
         loading: false,
         error: "Sth went wrong."
       });
     }
   };
 
   fetchData();
 }, []);
 
 return fetchedData;
};
 
// Second hook

interface Post {
 id: number;
 title: string;
 body: string;
 userId: number;
}
 
// Interface which describes data returned by usePostsFetch hook 
interface ReturnedPostsData {
 data?: Post[];
 error?: string;
 loading: boolean;
}
 
export const usePostsFetch = (): ReturnedPostsData => {
 const [fetchedData, setFetchedData] = React.useState<ReturnedPostsData>({
   loading: true
 });
 
 React.useEffect(() => {
   const fetchData = async (): Promise<void> => {
     try {
       const { data } = await axios.get(`${base}/posts`);
 
       setFetchedData({ data, loading: false, error: undefined });
     } catch {
       setFetchedData({
         data: undefined,
         loading: false,
         error: "Sth went wrong."
       });
     }
   };
 
   fetchData();
 }, []);
 
 return fetchedData;
};
```
<p>We have 2 separate hooks to get users and posts data. </p>
<p>It is not written with DRY rule, so how we can optimize our code to be more reusable if we want to make more requests in our app?</p>
<p>Let’s check the difference between following hooks and move them to arguments of our new reusable hook.<br><br>First of all, let's analyze the differences. </p>
<p>As we can see there are a few dissimilar things like url to the endpoint, returned type of data, and hook state type. So what we can do with it?</p>
<p>We need to create a custom hook that we will be calling useFetch with reusable logic. This hook will have one argument which is url. </p>
<p>But how we can pass specific type of returned data to our logic? </p>
<p>Well, our superheroes are GENERICS TYPES. </p>
<p>Let’s do it.</p>
```javascript
/* ReturnedData<T> is an interface which describes data returned by 
hook. Here we use previous interface body but we need to add generics 
type. Thanks to that it is more reusable and data can be any of type
passed as T.*/
interface ReturnedData<T> {
  data?: T;
  error?: string;
  loading: boolean;
}
 
// FetchedData is a type passed to useFetch during calling a hook.
export const useFetch = <FetchedData>(
  url: string
// ReturnedData<FetchedData> - We pass here data type to our generic
// interface.
): ReturnedData<FetchedData> => {
  const [fetchedData, setFetchedData] = React.useState<
    ReturnedData<FetchedData>
  >({
    loading: true
  });
 
  React.useEffect(() => {
    const fetchData = async (): Promise<void> => {
      try {
        // Static url replaced by dynamic param passed to hook 
        const { data } = await axios.get(`${base}${url}`);
 
        setFetchedData({ data, loading: false, error: undefined });
      } catch {
        setFetchedData({
          data: undefined,
          loading: false,
          error: "Sth went wrong."
        });
      }
    };
 
    fetchData();
  }, []);
 
  return fetchedData;
};
```
<p>During invoking the hook we should pass a returned data type between <code>&lt;&gt;</code> syntax like <code>&lt;Users[]&gt;.&nbsp;</code></p>
<p><code>const users = useFetch&lt;User[]&gt;("/users")</code>.<br><code>const posts = useFetch&lt;Post[]&gt;("/posts")</code>.</p>
<p>Finally, our code is clarified, reusable and well-typed. </p>
<p>You can review the created code on CodeSandbox.<br><br>Link here:<br><a rel="noreferrer noopener" aria-label="https://codesandbox.io/s/dreamy-cloud-oocxq?eslint=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark (opens in a new tab)" href="https://codesandbox.io/s/dreamy-cloud-oocxq?eslint=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" target="_blank"><code>https://codesandbox.io/s/dreamy-cloud-oocxq?eslint=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark</code></a><a href="https://codesandbox.io/s/dreamy-cloud-oocxq?fontsize=14&amp;hidenavigation=1&amp;theme=dark"></a><a href="https://codesandbox.io/s/dreamy-cloud-oocxq?fontsize=14&amp;hidenavigation=1&amp;theme=dark"></a></p>
<h2><b>Summary</b></h>
<p>To sum up, using Generics in Typescript gives you the ability to pass a huge range of types to a component and add an additional abstraction layer to your code which makes it being written with DRY rule. </p>
<p>Remember that we can apply generics to functions, interfaces and classes in Typescript.</p>
<p>I hope this example helped you to understand what generics are, how we can use them and why.</p>