Skip to content

(suggestion) tagged union types #10253

Closed
Closed
@negatratoron

Description

@negatratoron

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions