title: How to deal with 'Object is possibly undefined' error in TypeScript
body: <p>I have a function where a param may be a string or undefined, but then a validation check that throws an error if it's undefined. Any further code is guaranteed that the param is defined, but I'm now fighting against the TypeScript casting of it potentially being undefined. What's the nice way in TypeScript to deal with this issue?</p>
  
  <pre class="lang-js prettyprint-override"><code>import { validatePetFields } from "../models/pets";
  
  interface controllerParams {
     req: {
        petName?: string;
     };
  }
  
  export default async ({ req }: controllerParams): Promise&lt;any&gt; =&gt; {
  
     validatePetFields(req, [
        "petName",
     ]); // this function intentionally throws a JS error if petName is undefined
  
     console.log(req.petName.charAt(0).toUpperCase() + req.petName.slice(1)); // throws TypeScript error: Object is possibly 'undefined'
  };
  </code></pre>