Type Utilities [NOT COMPLETE]
[WARNINGS]
- Since the new type solver is still in development, not all of the features will remain constant.
- The names for the functions are also subject to change to make them more understandable, especially those that conflict with existing supported functions.
Purpose
The package helps speed up the process of debugging user-defined types, it also provides (or will*) necessary types to speed up the process of typing components, which allows for more time to be spent pondering over the design of the type rather than the implementation of the type.The package is designed with the future in mind, not the present, so it will (currently) require copy/pasting some of the user-defined function types if you want to use them within your own user-defined function types. Otherwise you can treat them like generic types. Purely Typed Luau, so none of the code will do anything when run.
The user-defined type functions I have provided are NOT optimized and may have bugs. If you spot any bugs or have advice on how to make it faster or think that better names would suit certain functions, feel free to reply. Suggestions are most appreciated.
Utils
Name_<_List[Types]_>_ | Description |
---|---|
equal< A, B > | Whether two types are equivalent, it doesn’t matter whether it is a table, a singleton, or a function, everything is considered. Returns a boolean singleton type. |
negate< A > | Returns the negation of type a. |
union< ... > | Returns the union of the provided types. |
assert< A > | Throws an error if the provided type is false or nil. |
assertf< A, B > | Throws an error with the specified message (B) if the provided type is false or nil. |
printType< A > | Prints the variant of the type, whether it is a singleton, class, table, or anything. |
prettyPrint< A > | Pretty prints the type, whether it is a singleton, table, cyclic function, anything. |
Util Examples
equal
-- Rarely used as types in practice, mainly used in user-defined types for validity
type Sample1 = equal<{[{a: number}]: number}, {[{a: number}]: number}>
type Sample2 = equal<<A,C,D,F...>(F...)->(A,C,D), <A,C,D,F...>(F...)->(A,C,D)>
type Sample3 = equal<{a: "Hello"}, {a: "hello"}>
local sample1: Sample1 -- true singleton type
local sample2: Sample2 -- true singleton type
local sample3: Sample3 -- false singleton type
negate
-- the type is not a string
type Sample = negate<string>
assert
-- Shows assertion failed
type Sample = assert<equal<"Hello", "World">>
assertf
-- Shows Unable to match values
type Sample = assertf<equal<"Hello", string>, "Unable to match values">
printType
-- Shows the type is singleton
type Sample = printType<"Hello World!">
prettyPrint
-- Shows "Hello World!"
type Sample = prettyPrint<"Hello World!">
Set
Name_<_List[Types]_>_ | Description |
---|---|
constrain< A, B > | Throws an error is the constrained type A doesn't fit into the constraint type B. |
Set Examples
constrain
type Fruit = "Fruit" | string
type ConstrainToFruit<A> = constrain<A, Fruit>
-- Shows that "Apple cannot fit inside Fruit",
-- BUT since "Apple" is a string, will still return itself
-- Only worry when the returned type is never
type Apple = ConstrainToFruit<"Apple">
How to Use
You can download the package here:
TypeUtils0.2.3.rbxm (15.1 KB)
You must first ensure that the New-Type Solver beta feature is enabled.
To access the Util type functions, require TypeUtilities/variant/Utils
.
To access the Set type functions, require TypeUtilities/variant/Set
.
I will try to find a way to use TypeUtilities
to store all of the types for convenience, but it may not be possible.
Plans
- Table accessing, combining, and setting functions (may include some aspects of table library)
- Boolean logical functions (or, and, xor, …)
- Number arithmetic functions (math library and bit32 library and <= / <)
- String base functions (just string library)