title: In Typescript how do you get an element type from an array type, when the array contains different element types?
body: <p>Let's assume I have an array of data that is of different types:</p>
  
  <pre><code>const input = ["random", 2, {a: "foo", b: "baz"}];
  </code></pre>
  
  <p>Typescript can automatically infer and extract the types, so for example:</p>
  
  <pre><code>type InputTypeArray = typeof input;
  </code></pre>
  
  <p>Here typescript knows that InputTypeArray is (string | number | {a: string, b: string})[];</p>
  
  <p>Let's call InputType the type of the elements of the array, that is:</p>
  
  <pre><code>typeof InputType = string | number | {a: string, b: string};
  </code></pre>
  
  <p>Can I derive InputType from InputTypeArray?
  In other words, how I would write a function that accept one of the elements of the input as parameter?</p>
  
  <pre><code>const input = ["random", 2, {a: "foo", b: "baz"}];
  
  const myFunction = (element: InputType) =&gt; element;
  
  input.map(myFunction);
  </code></pre>
  
  <p>How do I write "InputType" here ? is there a way to extract it from input?</p>
  
  <p>I know I could workaround by inlining myFunction inside the map call, as typescript will again infer the correct types inside the function. But is there a way to get the type neverthless?</p>