title: Inline conditional typeguard
body: <p>I want to be able to accept either a string or an error for one of the arguments to my class, but then have a type guard such that when I use an inline conditional to check the type, it compiles. </p>
  
  <p>It seems that typeguards for inline conditionals aren't supported, as evidenced from the three examples below, only the last of which compiles. </p>
  
  <p>When I say "inline conditionals", I mean of the form <code>(&lt;boolean statement&gt;)? a : b</code> , not multiline if/else blocks, which for some reason are referred to as "inline" in the <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html" rel="nofollow noreferrer" title="documentation">documentation</a></p>
  
  <p>UPDATE: this is more to do with javascript than typescript, which does support type guards for inline conditionals properly</p>
  
  <pre><code>export class MyError extends Error {
      public code; name; message
  
      constructor(code: number = -1, name: string = '', err?: Error | string) {
          super()
          this.name = name
          this.code = code 
          // no type guards
          this.message = this.name + (err)? ": " + (err instanceof Error)? err.message : err : ''
      }
  }
  
  export class MyError2 extends Error {
      public code; name; message
  
      constructor(code: number = -1, name: string = '', err?: Error | string) {
          super()
          this.name = name
          this.code = code 
          if(err) {
              // no type guards
              this.message = this.name + ": " + (err instanceof Error)? err.message : err
          } else {
              this.message = this.name
          }
      }
  }
  
  export class MyError3 extends Error {
      public code; name; message
  
      constructor(code: number = -1, name: string = '', err?: Error | string) {
          super()
          this.name = name
          this.code = code 
          // type guards... but at what cost to brevity
          this.message = this.name
          if(err) {
              if(err instanceof Error) {
                  this.message += ": " + err.message
              } else {
                  this.message += ": " + err
              }
          }
      }
  }
  </code></pre>