title: Typescript, JSON.parse error: "Type 'null' is not assignable to type 'string'."
body: <p>The error was happening here:</p>
  
  <pre><code>let moonPortfolio;
  ...
  moonPortfolio = JSON.parse(localStorage.getItem('moonPortfolio'));
  </code></pre>
  
  <p>I found <a href="https://stackoverflow.com/questions/46915002/argument-of-type-string-null-is-not-assignable-to-parameter-of-type-string">this answer</a> which makes sense, however I'm still getting that error after this refactor:</p>
  
  <blockquote>
    <p>As the error says, localStorage.getItem() can return either a string or null. JSON.parse() requires a string, so you should test the result of localStorage.getItem() before you try to use it.</p>
  </blockquote>
  
  <pre><code>if (portfolio.length === 0) {
    const storedPortfolio = localStorage.getItem('moonPortfolio');
  
    if (typeof storedPortfolio === 'string') {
      moonPortfolio = JSON.parse(localStorage.getItem('moonPortfolio'));
    }
    else {
      moonPortfolio = [];
    }
  
    if (moonPortfolio) {
      const savedPortfolio = Object.values(moonPortfolio);
      this.props.fetchAllAssets();
      // this.props.addCoins(savedPortfolio);
    }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/qn4hK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qn4hK.png" alt="enter image description here"></a></p>
  
  <p>I first set the results of localStorage <code>moonPortfolio</code> to a var, then check if the var is <code>typeof</code> string. Yet still getting the typescript error?</p>
  
  <p>Any thoughts or direction here?</p>