<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Prasanth M</title>
    <description>The latest articles on Forem by Prasanth M (@prasanth94).</description>
    <link>https://forem.com/prasanth94</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F689788%2F6974c021-1d14-4cca-b616-ab9183907898.jpeg</url>
      <title>Forem: Prasanth M</title>
      <link>https://forem.com/prasanth94</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prasanth94"/>
    <language>en</language>
    <item>
      <title>Typescript Generics</title>
      <dc:creator>Prasanth M</dc:creator>
      <pubDate>Sun, 26 Mar 2023 10:20:18 +0000</pubDate>
      <link>https://forem.com/prasanth94/typescript-generics-5gf8</link>
      <guid>https://forem.com/prasanth94/typescript-generics-5gf8</guid>
      <description>&lt;p&gt;Here In this article, I wanted to explain on how typescript generics works for a react component with an example. Before going to that, lets understand what exactly is Generics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Generics ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generics are a way to write reusable code that works with multiple types, rather than just one specific type.&lt;/p&gt;

&lt;p&gt;This means that instead of writing separate functions or components for each type we want to work with, we can write a single generic function or component that can handle any type we specify when we use it.&lt;/p&gt;

&lt;p&gt;For example, let's say you want to write a function that returns the first element of an array. You could write a separate function for each type of array you might encounter, like an array of numbers, an array of strings, and so on. But with generics, you can write a single function that works with any type of array.&lt;/p&gt;

&lt;p&gt;Generics are indicated using angle brackets (&amp;lt; &amp;gt;) and a placeholder name for the type. For example, you might define a generic function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function firstItem&amp;lt;T&amp;gt;(arr: T[]): T {
  return arr[0];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt; in the function signature indicates that this is a generic function, and T is the placeholder name for the type. Now you can use this function with any type of array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];
const strings = ['a', 'b', 'c'];

console.log(firstItem(numbers)); // 1
console.log(firstItem(strings)); // 'a'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just a simple example, but generics can be used in much more complex scenarios to write highly flexible and reusable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we use it for react components ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Animal&amp;lt;T extends string&amp;gt; {
  species: T
  name: string
  // Define other properties as needed
}

// Define custom types that extend the Animal type
interface Cat extends Animal&amp;lt;'cat'&amp;gt; {
  color: string
}

interface Dog extends Animal&amp;lt;'dog'&amp;gt; {
  breed: string
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing we see is the definition of the &lt;code&gt;Animal&lt;/code&gt; interface, which is a generic type that extends multiple custom types. The &lt;code&gt;T&lt;/code&gt; type parameter is used to specify the species of the animal, which can be either 'cat' or 'dog'. The &lt;code&gt;Cat&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt; interfaces are custom types that extend the &lt;code&gt;Animal&lt;/code&gt; type and add additional properties like &lt;code&gt;color&lt;/code&gt; and &lt;code&gt;breed&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define the state type as an object with a "data" property of type Animal or null
interface State&amp;lt;T extends Animal&amp;lt;string&amp;gt; | null&amp;gt; {
  data: T
}


// Define the action types for useReducer
type Action&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt; =
  | { type: 'SET_DATA'; payload: T }
  | { type: 'CLEAR_DATA' }
  | { type: 'CHANGE_SPECIES'; payload: string }

// Define the reducer function for useReducer
function reducer&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt;(state: State&amp;lt;T&amp;gt;, action: Action&amp;lt;T&amp;gt;): State&amp;lt;T&amp;gt; {
  switch (action.type) {
    case 'SET_DATA':
      return { ...state, data: action.payload }
    case 'CLEAR_DATA':
      return { ...state, data: null }
    case 'CHANGE_SPECIES':
      if (state.data) {
        return {
          ...state,
          data: { ...state.data, species: action.payload },
        }
      }
      return state
    default:
      throw new Error(`Unhandled action : ${action}`)
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we see the definition of the &lt;code&gt;State&lt;/code&gt; interface, which is also a generic type that accepts any type that extends the &lt;code&gt;Animal&lt;/code&gt; type or &lt;code&gt;null&lt;/code&gt;. This interface defines an object with a single property &lt;code&gt;data&lt;/code&gt;, which can be either of the generic type or null.&lt;/p&gt;

&lt;p&gt;After that, we define the &lt;code&gt;Action&lt;/code&gt; type, which is also a generic type that accepts any type that extends the &lt;code&gt;Animal&lt;/code&gt; type. This type specifies the different actions that can be dispatched by the reducer function. In this case, there are three possible actions: &lt;code&gt;SET_DATA&lt;/code&gt;, &lt;code&gt;CLEAR_DATA&lt;/code&gt;, and &lt;code&gt;CHANGE_SPECIES&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reducer function itself is also a generic function that accepts two parameters: the state object of type &lt;code&gt;State&amp;lt;T&amp;gt;&lt;/code&gt; and the action object of type &lt;code&gt;Action&amp;lt;T&amp;gt;&lt;/code&gt;. The T type parameter is used to specify the generic type that is passed to the &lt;code&gt;State&lt;/code&gt; and &lt;code&gt;Action&lt;/code&gt; interfaces. The reducer function is responsible for handling the different actions and updating the state accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Create a context for the state and dispatch functions to be passed down to child components
interface AnimalContextType&amp;lt;T extends Animal&amp;lt;string&amp;gt; | null&amp;gt; {
  state: State&amp;lt;T&amp;gt;
  dispatch: React.Dispatch&amp;lt;Action&amp;lt;T&amp;gt;&amp;gt;
}

const AnimalContext = createContext&amp;lt;AnimalContextType&amp;lt;Cat | Dog | null&amp;gt;&amp;gt;({
  state: { data: null },
  dispatch: () =&amp;gt; {},
})

interface AnimalProviderProps {
  children: React.ReactNode
}

// Define a component that fetches data from an API and updates the state
function AnimalProvider({ children }: AnimalProviderProps) {
  const [state, dispatch] = useReducer(reducer, { data: null } as State&amp;lt;Cat | Dog | null&amp;gt;)

  useEffect(() =&amp;gt; {
    // Fetch data from the API and update the state
    // You'll need to replace the URL with the actual API endpoint
    fetch('https://example.com/api/animal')
      .then((response) =&amp;gt; response.json())
      .then((data: Cat | Dog) =&amp;gt; {
        // Update the state with the fetched data
        dispatch({ type: 'SET_DATA', payload: data })
      })
      .catch((error) =&amp;gt; {
        console.error(error)
      })
  }, [])

  return (
    &amp;lt;AnimalContext.Provider value={{ state, dispatch }}&amp;gt;
      {/* Render child components */}
      {children}
    &amp;lt;/AnimalContext.Provider&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;AnimalContextType&lt;/code&gt; interface is another generic interface that specifies the shape of the context object. It accepts any type that extends the Animal type or null. This interface defines two properties: &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;dispatch&lt;/code&gt;, which are used to manage the state and dispatch actions.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AnimalProvider&lt;/code&gt; component is a regular React component that wraps the &lt;code&gt;AnimalComponent&lt;/code&gt; component and provides access to the context object. It uses the useReducer hook to manage the state and the useEffect hook to fetch data from an API and update the state accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AnimalComponent&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt;() {
  const { state, dispatch } = useContext(AnimalContext)

  const handleChangeSpecies = (event: React.ChangeEvent&amp;lt;HTMLSelectElement&amp;gt;) =&amp;gt; {
    dispatch({ type: 'CHANGE_SPECIES', payload: event.target.value })
  }

  return (
    &amp;lt;div&amp;gt;
      {state.data &amp;amp;&amp;amp; (
        &amp;lt;div&amp;gt;
          &amp;lt;h2&amp;gt;{state.data.name}&amp;lt;/h2&amp;gt;
          &amp;lt;p&amp;gt;Species: {state.data.species}&amp;lt;/p&amp;gt;
          {state.data.species === 'cat' &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Color: {(state.data as Cat).color}&amp;lt;/p&amp;gt;}
          {state.data.species === 'dog' &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Breed: {(state.data as Dog).breed}&amp;lt;/p&amp;gt;}
          &amp;lt;select value={state.data.species} onChange={handleChangeSpecies}&amp;gt;
            &amp;lt;option value='cat'&amp;gt;Cat&amp;lt;/option&amp;gt;
            &amp;lt;option value='dog'&amp;gt;Dog&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, the &lt;code&gt;AnimalComponent&lt;/code&gt; component is also a generic component that accepts any type that extends the Animal type. It uses the useContext hook to access the context object and render the state data. It also provides a dropdown menu to allow the user to change the species of the animal and dispatch the &lt;code&gt;CHANGE_SPECIES&lt;/code&gt; action.&lt;/p&gt;

&lt;p&gt;In summary, what I demonstrated is how TypeScript generics can be used to write reusable code that can work with different types. By using generics, we can write a single component that can handle any type of animal, rather than writing separate components for each species.&lt;/p&gt;

&lt;p&gt;Full code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { createContext, useReducer, useEffect, useContext } from 'react'

// Define the Animal type as a generic type that extends multiple custom types

interface Animal&amp;lt;T extends string&amp;gt; {
  species: T
  name: string
  // Define other properties as needed
}

// Define custom types that extend the Animal type
interface Cat extends Animal&amp;lt;'cat'&amp;gt; {
  color: string
}

interface Dog extends Animal&amp;lt;'dog'&amp;gt; {
  breed: string
}

// Define the state type as an object with a "data" property of type Animal or null
interface State&amp;lt;T extends Animal&amp;lt;string&amp;gt; | null&amp;gt; {
  data: T
}

// Define the action types for useReducer
type Action&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt; =
  | { type: 'SET_DATA'; payload: T }
  | { type: 'CLEAR_DATA' }
  | { type: 'CHANGE_SPECIES'; payload: string }

// Define the reducer function for useReducer
function reducer&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt;(state: State&amp;lt;T&amp;gt;, action: Action&amp;lt;T&amp;gt;): State&amp;lt;T&amp;gt; {
  switch (action.type) {
    case 'SET_DATA':
      return { ...state, data: action.payload }
    case 'CLEAR_DATA':
      return { ...state, data: null }
    case 'CHANGE_SPECIES':
      if (state.data) {
        return {
          ...state,
          data: { ...state.data, species: action.payload },
        }
      }
      return state
    default:
      throw new Error(`Unhandled action : ${action}`)
  }
}

// Create a context for the state and dispatch functions to be passed down to child components
interface AnimalContextType&amp;lt;T extends Animal&amp;lt;string&amp;gt; | null&amp;gt; {
  state: State&amp;lt;T&amp;gt;
  dispatch: React.Dispatch&amp;lt;Action&amp;lt;T&amp;gt;&amp;gt;
}

const AnimalContext = createContext&amp;lt;AnimalContextType&amp;lt;Cat | Dog | null&amp;gt;&amp;gt;({
  state: { data: null },
  dispatch: () =&amp;gt; {},
})

interface AnimalProviderProps {
  children: React.ReactNode
}

// Define a component that fetches data from an API and updates the state
function AnimalProvider({ children }: AnimalProviderProps) {
  const [state, dispatch] = useReducer(reducer, { data: null } as State&amp;lt;Cat | Dog | null&amp;gt;)

  useEffect(() =&amp;gt; {
    // Fetch data from the API and update the state
    // You'll need to replace the URL with the actual API endpoint
    fetch('https://example.com/api/animal')
      .then((response) =&amp;gt; response.json())
      .then((data: Cat | Dog) =&amp;gt; {
        // Update the state with the fetched data
        dispatch({ type: 'SET_DATA', payload: data })
      })
      .catch((error) =&amp;gt; {
        console.error(error)
      })
  }, [])

  return (
    &amp;lt;AnimalContext.Provider value={{ state, dispatch }}&amp;gt;
      {/* Render child components */}
      {children}
    &amp;lt;/AnimalContext.Provider&amp;gt;
  )
}

function AnimalComponent&amp;lt;T extends Animal&amp;lt;string&amp;gt;&amp;gt;() {
  const { state, dispatch } = useContext(AnimalContext)

  const handleChangeSpecies = (event: React.ChangeEvent&amp;lt;HTMLSelectElement&amp;gt;) =&amp;gt; {
    dispatch({ type: 'CHANGE_SPECIES', payload: event.target.value })
  }

  return (
    &amp;lt;div&amp;gt;
      {state.data &amp;amp;&amp;amp; (
        &amp;lt;div&amp;gt;
          &amp;lt;h2&amp;gt;{state.data.name}&amp;lt;/h2&amp;gt;
          &amp;lt;p&amp;gt;Species: {state.data.species}&amp;lt;/p&amp;gt;
          {state.data.species === 'cat' &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Color: {(state.data as Cat).color}&amp;lt;/p&amp;gt;}
          {state.data.species === 'dog' &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Breed: {(state.data as Dog).breed}&amp;lt;/p&amp;gt;}
          &amp;lt;select value={state.data.species} onChange={handleChangeSpecies}&amp;gt;
            &amp;lt;option value='cat'&amp;gt;Cat&amp;lt;/option&amp;gt;
            &amp;lt;option value='dog'&amp;gt;Dog&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  )
}

// Finally, use the AnimalProvider component to wrap child components and provide access to the state and dispatch functions

function App() {
  return (
    &amp;lt;AnimalProvider&amp;gt;
      &amp;lt;AnimalComponent /&amp;gt;
    &amp;lt;/AnimalProvider&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>generics</category>
    </item>
  </channel>
</rss>
