title: Writing sanitizer function in Typescript to check for null, undefined and empty strings and getting error inside function
body: <p>I'm writing a generic function in Typescript to take a string value and check to see if it is null, undefined or an empty string and return null or if it's an actual value return it trimmed.</p>
  
  <p>The issue I'm running into ironically, is that inside the function on the second line at <code>return stringValue.trim();</code> I'm getting the error <code>Object is possibly null or undefined</code> and I'm not sure what kind of check I can do to fix that.</p>
  
  <p>This is my function currently:</p>
  
  <pre><code>  const sanitize = (stringValue: string | undefined | null) =&gt; {
      let emptyString = '';
      let nullValue = null;
      let undefinedValue = undefined;
  
      if(typeof stringValue === emptyString || typeof stringValue === nullValue || typeof stringValue === undefinedValue){
        return null
      } else {
        return stringValue.trim();
      }
    }
  </code></pre>