title: Common pattern when variable is used before being assigned
body: <p>I have a module</p>
  
  <pre><code>import pino, { Logger } from 'pino';
  
  let logger: Logger;
  
  if (process.env.NODE_ENV === 'production') {
      const dest = pino.extreme();
      logger = pino(dest);
  }
  
  if (process.env.NODE_ENV === 'development') {
      // @ts-ignore
      logger = pino({ prettyPrint: { colorize: true } });
  }
  
  ...
  
  export default logger;
  // ^^^ [ts] Variable 'logger' is used before being assigned. [2454]
  </code></pre>
  
  <p>Here is no any situation when logger is undefined, but even if it will be undefined it is suted for me</p>
  
  <p>How to resolve the TypeScript error at the end:</p>
  
  <blockquote>
    <p>Variable 'logger' is used before being assigned. [2454]</p>
  </blockquote>
  
  <p>I've rewritten my code but the error still here</p>
  
  <pre><code>import pino, { Logger } from 'pino';
  
  let logger: Logger;
  
  if (process.env.NODE_ENV === 'production') {
      const dest = pino.extreme();
      logger = pino(dest);
  } else 
  
  if (process.env.NODE_ENV === 'development') {
      // @ts-ignore
      logger = pino({ prettyPrint: { colorize: true } });
  } else
  
  if (process.env.NODE_ENV === undefined) {
      throw new Error('Logger must be initialized! Set up process.env.NODE_ENV');
  }
  
  if (logger) { // &lt;-- Variable 'logger' is used before being assigned. [2454]
     // configuring process
  }
  
  export default logger;
  </code></pre>
  
  <p>Even in another way</p>
  
  <pre><code>import pino, { Logger } from 'pino';
  
  let logger: Logger;
  
  function configureProcess(theLogger: Logger) {
    // setup process with theLogger
  }
  
  if (process.env.NODE_ENV === 'production') {
      const dest = pino.extreme();
      logger = pino(dest);
      configureProcess(logger); // &lt;-- code duplication
  } else if (process.env.NODE_ENV === 'development') {
      // @ts-ignore
      logger = pino({ prettyPrint: { colorize: true } });
      configureProcess(logger); // &lt;-- code duplication
  }
  
  if (process.env.NODE_ENV === undefined) {
      throw new Error('Logger must be initialized! Set up process.env.NODE_ENV');
  }
  
  export default logger;
  // ^^^ [ts] Variable 'logger' is used before being assigned. [2454]
  </code></pre>
  
  <p>I feel like a fighter against TypeScrit instead of developer - so many dances to solve a problem which is not a problem in reality (</p>