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
-
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.
-
printType< A >: Prints the variant of the type, whether it is a singleton, class, table, or anything.
-
prettyPrintType< A >: Pretty prints the type, whether it is a singleton, table, cyclic function, anything.
-
printKeysOfType<Table, Key>: Pretty prints specific key-value pairs whose key matches the variant of b, or b’s value if it is a singleton.
-
printValuesOfType<Table, Value>: Pretty prints specific key-value pairs whose value matches the variant of Value, or Value’s value if it is a singleton.
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>
printType
-- Shows the type is singleton
type Sample = printType<"Hello World!">
prettyPrintType
-- Shows "Hello World!"
type Sample = prettyPrintType<"Hello World!">
printKeysOfType
-- Shows {a: number}
type Sample = printKeysOfType<{a: number, b: string}, "a">
printValuesOfType
-- Shows {a: number}
type Sample = printValuesOfType<{a: number, b: string}, number>
How to Use
You can download the package here: TypeUtils.rbxm (11.0 KB)
You must first ensure that the New-Type Solver beta feature is enabled.
After that, you can find the Utils moduleScript in TypeUtilities/variant/Utils
, so require it. 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, mux, …)
- Number arithmetic functions (math library and bit32 library and <= / <)
- String base functions (just string library)