news-2022-03-31-luau-recap-march-2022.md
March 31, 2022
We added support for singleton types! These allow you to use string or boolean literals in types. These types are only inhabited by the literal, for example if a variable x has type "foo", then x == "foo" is guaranteed to be true.
Singleton types are particularly useful when combined with union types, for example:
or:
In particular, singleton types play well with unions of tables, allowing tagged unions (also known as discriminated unions):
The RFC for singleton types is https://github.com/Roblox/luau/blob/master/rfcs/syntax-singleton-types.md
A common idiom for programming with tables is to provide a public interface type, but to keep some of the concrete implementation private, for example:
Within a module, a developer might use the concrete type, but export functions using the interface type:
Previously examples like this did not typecheck but now they do!
This language feature is called width subtyping (it allows tables to get wider, that is to have more properties).
The RFC for width subtyping is https://github.com/Roblox/luau/blob/master/rfcs/sealed-table-subtyping.md
function f(x : any) can now be called as f() without a type error.table.clone which takes a table and returns a new table that has the same keys/values/metatable. The cloning is shallow - if some keys refer to tables that need to be cloned, that can be done manually by modifying the resulting table.