Does Haxe have something similar to Union & Intersection types in TypeScript?

To be clear where my “loyalties” lie: I’m a big Haxe fan and I am working on getting this language additional popularity through a major personal project. I quoted “loialties” because I’m no fanboy. I like Haxe because it’s a powerful TOOLKIT. No other platform will allow me to transpile to X other programming languages anytime soon. However, not being a fanboy, I’m not blind to what the competition is doing.

I’ve been doing some TypeScript at work and some features of the language have provoked my curiosity. Namely union & intersection types. They are both described here:

Also, this led me to discover a language called Ceylon, which has been around for some time:

I don’t think of this as a KILLER FEATURE, but definitely a nice to have, given some articles that I’ve read recently about how they’ve been using this in TypeScript. Here is one such article:

Nope, Haxe does not currently have formal intersection and union types. You can merge structure types with the intersection syntax: {a:Int} & {b:String} to {a:Int, b:String}, as well as type parameter constraints: <T:A & B>, but that’s it.

1 Like

I actually dislike the type juggling you can do in typescript.
Types should be simple and clear “hinters” and should enforce some constrains on your code.
With typescript implementation I find myself debug the type jungle instead of focus on my code

1 Like

Haxe isn’t a “structural type system” like TypeScript which is mean to work with the sort of objects and classes manipulations you do in JS (combine/transform/mix-in). It’s generally not recommended (bad for performance) for most of the other Haxe targets :smiley: which is why the language doesn’t have a big emphasis on this kind of typing…

1 Like

More constructively, aside from dubious cases of unions used for some sort of overloading, most practical cases of union in TypeScript are usually efficiently solved using Haxe Enums.

Intersection types are a bit of an heresy in nominal type systems like Haxe - this is guaranteed to be extremely inefficient in languages like C++, Java, C#, etc.

But I would certainly like to see more options to manipulate structure types, like TypeScript’s “mapped types”, allowing clean Partial<T> (make all fields optional), or transforming/plucking JSON objects.

Agreed that this may be a footgun :smiley:.

@elsassph: well explained regarding TypeScript’s purpose. I guess it’s called TypeScript for a reason eh? :slight_smile:

Still, allowing all sorts of type acrobatics may end up a beneficial thing when one wants to allow more complex interactions among types at the cost of performance. But ok, let’s leave this for when the use case presents itself :smiley:

ShockScript supports structural types, including unions, functions and tuples, just like in ECMAScript 4.

var v:(String,RegExp) = /(:)/gi

switch type (v)
    case ({source:p}:RegExp) { trace(p) }

Because Haxe is similiar to ECMAScript, ShockScript (ES3 dialect) might be an option. Enumeration types are numeric only though (not algebraic data structures like in Rust or Haxe) and contextually typed as literal strings.

enum T
    const X
        , Y
        , Z

var v:T = 'x' | 'y'
trace( 'x' in v , uint( v ) )

Interesting :smiley: . I see it is written in JavaScript. Any documentation on architecture, purpose, performance, future targets?