Closed
Description
Javascript objects generally represent tagged intersection types, for example {a : Number, b : Number }
, the intersection of two numbers.
Tagged union types can also be represented using Javascript objects. An instance of a tagged union type has one property, of the specified type. Values of a tagged union type can only be used in a program through case splitting. Here's a quick code example:
var toString : (x : oneOf({
a : Number,
b : Number,
})) => String = caseSplit({
a: function (x) {
return "a: " + x;
},
b: function (x) {
return "b: " + x;
},
});
var a = toString({
a: 3
}); // "a: 3"
var b = toString({
b: 4
}); // "b: 4"
var c = toString({
c: 5
}); // error