Union and Intersection Types
Union types
Section titled “Union types”A union type represents one of the types in this set. If you try to pass a union onto another thing that expects a more specific type, it will fail.
For example, what if this string | number was passed into something that expects number, but the passed in value was actually a string?
Note: it’s impossible to be able to call a function if there are two or more function types in this union.
Tagged unions
Section titled “Tagged unions”Tagged unions are just union types! In particular, they’re union types of tables where they have at least some common properties but the structure of the tables are different enough. Here’s one example:
This Result<T, E> type can be discriminated by using type refinements on the property type, like so:
Which works out because value: T exists only when type is in actual fact "ok", and error: E exists only when type is in actual fact "err".
Intersection types
Section titled “Intersection types”An intersection type represents all of the types in this set. It’s useful for two main things: to join multiple tables together, or to specify overloadable functions.
Note: it’s impossible to create an intersection type of some primitive types, e.g. string & number, or string & boolean, or other variations thereof.
Note: Luau still does not support user-defined overloaded functions. Some of Roblox and Lua 5.1 functions have different function signature, so inherently requires overloaded functions.