Typescript : Adding types to a function that takes in a string and returns a different promise based on the string I have a function that takes in a string and based on the string hits different APIs to fetch images.
I have solved it by writing an interface and then as the return type of the function, it returns a Promise&lt;Interface&gt; as shown in the code below.
    interface IAnimal {
      image?: string | null
      message?: string
      status: string
    }
    const getAnimal = async (inAnimal: string): Promise&lt;IAnimal&gt; =&gt; {
      const isDogWord: boolean = [&quot;dog&quot;, &quot;Dog&quot;, &quot;DOG&quot;].includes(inAnimal.trim())
      const isFoxWord: boolean = [&quot;fox&quot;, &quot;Fox&quot;, &quot;FOX&quot;].includes(inAnimal.trim())
      let result: IAnimal = { image: null, status: &quot;tofetch&quot; };
      if(isDogWord) {
        result = await fetch(&quot;https://dog.ceo/api/breeds/image/random&quot;).then((data) =&gt; data.json());
        if(result.status === &quot;success&quot;) {
          return {
            image: result.message,
            status: result.status
          }
        }
      }
      if(isFoxWord) {
        result = await fetch(&quot;https://randomfox.ca/floof/&quot;).then((data) =&gt; data.json());
        return {
          image: result.image,
          status: &quot;success&quot;
        }
      }
      return {
        image: null,
        status: &quot;failure&quot;
      }
    };

This solves the typescript errors but I am not happy with making the image and message optional in the Interface. I am receiving { &quot;message&quot;: &quot;image__link&quot;, &quot;status&quot;: &quot;success or failure&quot; } from dog API and {&quot;image&quot;: &quot;image__link&quot;, &quot;status&quot;: &quot;success or failure&quot;} from the fox API. Is what I have done the right approach or do we have a better solution.
The problem here is that the interface IAnimal is not what those apis return. You want to return that interface from your function, but those APIs should have their own interface representing the response data.
interface DogApiResponse {
  message: string
  status: string
}

interface FoxApiResponse {
  image: string
}

interface Animal {
  status: string
  image: string | null
}

To generalize the set of available animal types I propose to also add interface for the fetching part of individual APIs.
declare type RandomAnimalFactory = () =&gt; Promise&lt;Animal&gt;

Now we declare two factories - for dogs and foxes
const dogFactory: RandomAnimalFactory = async () =&gt; {
  const result: DogApiResponse = await fetch(dogApiUrl).then(data =&gt; data.json())
  if (result.status !== 'success') {
    throw new Error('Dog API railure')
  }
  return {image: result.message, status: 'success'}
}

const foxFactory: RandomAnimalFactory = async () =&gt; {
  const result: FoxApiResponse = await fetch(foxApiUrl).then(data =&gt; data.json())
  return {image: result.image, status: 'success'}
}

The we can have the named animal factory work like this
declare type NamedAnimalFactory = (name: string) =&gt; Promise&lt;Animal&gt;


const createRandomNamedAnimalFactory = (randomFactories: {[key: string]: RandomAnimalFactory}): NamedAnimalFactory =&gt; {
  return async (name) =&gt; {
    const factory = randomFactories[name.trim().toLowerCase()]
    if (!factory) {
      return {image: null, status: 'failure'}
    }

    try {
      return await factory()
    } catch {
      return {image: null, status: 'failure'}
    }
  }
}

const getAnimal: NamedAnimalFactory = createRandomNamedAnimalFactory({
  dog: dogFactory,
  fox: foxFactory,
})
```