2

I'd like to have strictly typed mutable objects, exactly like the Haxe enums.

In haxe, you can do

enum Color {
  Red;
  Rgb(r:Int, g:Int, b:Int);
  Rgba(r:Int, g:Int, b:Int, a:Int);
}

I'd like to be able to access the a parameter only if my object is a Rgba, and I can't access any parameters if my object is a Red.

I don't really care if I use the enum keyword or not in typescript.

Is there any way to achieve that in Typescript ?

3
  • 1
    Do you really mean “mutable”? I don’t know Haxe, but enums are pretty much universally immutable in other languages. Anyway, what you’re describing may be called an enum in Haxe but is generally known under different names, such as as sum type or tagged union, of which enums are a special case. Commented Nov 17, 2016 at 14:16
  • How would you use it? Commented Nov 17, 2016 at 14:16
  • @KonradRudolph I really like that taggued union. How can I use that in typescript, in a type-safe way ? Commented Nov 17, 2016 at 14:20

1 Answer 1

2

As of version 2.0, Typescript supports tagged unions to some extent. The general syntax is

type MyType = A | B | C …;

Where A, B and C are interfaces. A MyType object can then be any of those types, and no other. The announcement gives a quick example:

interface Square {
    kind: "square";
    size: number;
}

interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}

interface Circle {
    kind: "circle";
    radius: number;
}

type Shape = Square | Rectangle | Circle;

function area(s: Shape) {
    // In the following switch statement, the type of s is narrowed in each case clause
    // according to the value of the discriminant property, thus allowing the other properties
    // of that variant to be accessed without a type assertion.
    switch (s.kind) {
        case "square": return s.size * s.size;
        case "rectangle": return s.width * s.height;
        case "circle": return Math.PI * s.radius * s.radius;
    }
}

However, the type safety if these union types needs to be guarded and checked manually with a so-called “discriminant property type guard” as the example shows (checking s.kind).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.