title: Why TypeScript doesn't infer the correct type when redeclaring a variable
body: <p>I was writing a code that goes a little bit like this:</p>
  <pre><code>function helloWorld(customName: string) {
    return `Hello, ${customName}`;
  }
  
  const myName: string | null = null;
  const isValidName = myName !== null;
  
  if (isValidName) {
    console.log(helloWorld(myName));
  }
  </code></pre>
  <p>If you <a href="https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABACwKYBt1wOpwE7oAmAFBCAM5RwC2AcgIbWoBcileMYA5gJSIDeAKESI8qKCDxIABgAkMWADSIAJPzKUaDJgF9pAbkE7BgiAkqJqAT20s2UDt0QAfRGBCZEAXjcf0hszALGHIANXp0GEJbb0sbRlREAEIvH3dMQ0EYYERiEPDI6IS%20IRFA8jh0VAA6LC5iNEwcfCJia1seHkMdIA" rel="nofollow noreferrer">run this code in the TypeScript playground</a>, you'll see TypeScript will complain that <code>Argument of type 'null' is not assignable to parameter of type 'string'.</code></p>
  <p>How's that the case, though? That code only runs when <code>isValidName</code> is truthy and <code>isValidName</code> can only be <code>true</code> if <code>myName</code> is not <code>null</code>. Hence, <code>myName</code> is a <code>string</code> here. Why this doesn't work?</p>