Closed
Description
I don't understand what is wrong with the following piece of code. It's a very basic scenario that I wish I could have since now union types are here. Please let me know what I am doing wrong:
interface Y { 'i am a very certain type': Y }
var y : Y = <Y> undefined;
function destructure<a, r>(
something: a | Y,
haveValue: (value: a) => r,
haveY: (value: Y) => r
) : r {
return something === y ? haveY(y) : haveValue(<a> something);
}
var value = Math.random() > 0.5 ? 'hey!' : <Y> undefined;
console.log(destructure(value, text => 'string', y => 'other one')); // <-- complete mess and failure
Related question at StackOverflow: http://stackoverflow.com/questions/28931221/whats-the-use-of-union-types-in-typescript