title: Typing a generic function which retrieves data from objects and (deep) nested objects in Typescript
body: <p>I have created a generic function using ES6 in the past and want to reuse it in a new React Typescript project.</p>
  <p>The function is called getNestedValuesFromObject and retrieves data from objects and (deep) nested objects.</p>
  <pre><code>const getValueFromNestedObject = (objectPath, baseObject) =&gt; {
      if (typeof objectPath !== 'string') return undefined;
      return objectPath
          .split('.')
          .reduce((object, key) =&gt; (object &amp;&amp; object[key] !== 'undefined' ? object[key] : undefined),
              baseObject);
  };
  
  export default getValueFromNestedObject;
  </code></pre>
  <p>And can be used as follows:</p>
  <pre><code>const testObject = {
    exampleString: 'string',
    nestedObject: {
     exampleNumber: 1,
     nestedNestedObject: {
       exampleArray: [1, 2, 3],
     }
   }
  }
  
  const arrayFromNestedNestedObject = getValueFromNestedObject('nestedObject.nestedNestedObject', testObject);
  </code></pre>
  <p>Can you help type this function?
  Obviously objectPath is string and baseObject can maybe be a generic also the return type.</p>