Skip to content

Instantly share code, notes, and snippets.

@wwahammy
Created May 18, 2018 17:05
Show Gist options
  • Save wwahammy/da70bcb62d44e3b7293c14397e0af317 to your computer and use it in GitHub Desktop.
Save wwahammy/da70bcb62d44e3b7293c14397e0af317 to your computer and use it in GitHub Desktop.
function a() : string |number
{
return ""
}
function b(){
let foo = a()
if( typeof foo === "number")
{
console.log(foo.toFixed())
}
if (typeof foo === "string")
{
console.log(foo.toLowerCase())
}
}
@wwahammy
Copy link
Author

In fact, this also compiles fine

function b(){
  let foo = a()
  if( typeof foo === "number")
  {
      console.log(foo.toFixed())
  }
  else
  {
      console.log(foo.toLowerCase())
  }
}

@seiyria
Copy link

seiyria commented May 18, 2018

class Character {

  constructor(public name: string, public x: number, public y: number) { }
}

class Butts {
  static doThing(isCoordinate: boolean): { x: number, y: number } | Character {
    if (isCoordinate) return { x: 1, y: 1 };

    return new Character('test', 1, 1);
  }
}

const res = Butts.doThing(true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment