90+ utility user-defined type functions for luau.
Requires the following fflags to be enabled:
{
"LuauTypeSolverRelease": "647",
"LuauSolverV2": "true",
"LuauUserDefinedTypeFunctionsSyntax2": "true",
"LuauUserDefinedTypeFunction2": "true"
}
(Because type functions can't currently be exported and imported into other files, all of the source code and the tests are co-located.)
Core Types
Outputs the inputted type but only with specificied components/properties.
Name | Type | Description |
---|---|---|
input | any | The type to pick components/properties from. |
toPick | any | The union of types (or a singleton/primitive) to be picked. |
type TypeResult = Pick<
"hello" & "world" & "foo" & "bar",
"world" | "bar"
>
-- type TypeResult = "bar" & "world"
Outputs the inputted type but only with components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The type to pick components/properties from. |
toPick | any | The union of types (or a singleton/primitive) to pick from. |
type TypeResult = PickExtends<
"hello" & boolean & "world" & true & string,
boolean
>
-- type TypeResult = boolean & true
Outputs the inputted type but with specificied components/properties removed.
Name | Type | Description |
---|---|---|
input | any | The type to omit components/properties from. |
toOmit | any | The union of types (or a singleton/primitive) to be omitted. |
type TypeResult = Omit<
"hello" | "world" | "foo" | "bar",
"world" | "bar"
>
-- type TypeResult = "foo" | "hello"
Outputs the inputted type but without components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The type to omit components/properties from. |
toOmit | any | The union of types (or a singleton/primitive) to be omitted. |
type TypeResult = OmitExtends<
"hello" | boolean | "world" | true | string,
boolean
>
-- type TypeResult = "hello" | "world" | string
Removes duplicate components from unions and intersections including inside table keys, values and indexers.
Name | Type | Description |
---|---|---|
input | any | The type to clean. |
type TypeResult = Clean<{
age: number | number,
[boolean]: boolean | boolean
}>
--[[
type TypeResult = {
[boolean]: boolean,
age: number
}
]]
Recursively flattens intersections, unions and intersections of tables into one consolidated type.
Name | Type | Description |
---|---|---|
input | any | The type to flatten. |
type TypeResult = Flatten<({ hello: "world" } & ({ foo: "bar" } | { lol: "kek" }))>
--[[
type TypeResult = {
foo: "bar",
hello: "world"
} | {
hello: "world",
lol: "kek"
}
]]
Outputs true
if the two inputted types are identical.
Name | Type | Description |
---|---|---|
inputA | any | The first type to compare. |
inputB | any | The second type to compare. |
type TypeResult = Equals<{ Kind: "Customer" }, { Kind: "Employee" }>
-- type TypeResult = false
Outputs the properties/components which exist in both inputA
and inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first type. |
inputB | any | The second type. |
type TypeResult = Overlap<"hello" & "world" & "foo", "lol" & "foo" & "world">
-- type TypeResult = "foo" & "world"
Outputs the properties/components which only exist in inputA
, and which only exist in inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first type. |
inputB | any | The second type. |
type TypeResult = Diff<"hello" | "world", "hello" | "foo">
-- type TypeResult = "foo" | "world"
Converts a type to camel case (camelCase).
Name | Type | Description |
---|---|---|
input | any | The type to convert to camel case. |
type TypeResult = ToCamel<{ Name: string, Age: number }>
-- type TypeResult = { age: number, name: string }
Converts a type to pascal case (PascalCase).
Name | Type | Description |
---|---|---|
input | any | The type to convert to pascal case. |
type TypeResult = ToPascal<"hello" & "world">
-- type TypeResult = "Hello" & "World"
Converts a type to upper case (UPPERCASE).
Name | Type | Description |
---|---|---|
input | any | The type to convert to upper case. |
type TypeResult = ToUpper<"Foo" | "Bar" | "lol">
-- type TypeResult = "BAR" | "FOO" | "LOL"
Converts a type to lower case (lowercase).
Name | Type | Description |
---|---|---|
input | any | The type to convert to lower case. |
type TypeResult = ToLower<{ HELLO: "world", FOO: "BAR" }>
--[[
type TypeResult = {
foo: "BAR",
hello: "world"
}
]]
Returns true if the inputted type is empty.
Name | Type | Description |
---|---|---|
input | any | The type to test emptiness for. |
type TypeResult = IsEmpty<{ }>
-- type TypeResult = true
Outputs a union of the inputted type and a nil singleton type.
Name | Type | Description |
---|---|---|
input | any | The type to make nilable. |
type TypeResult = Nilable<string>
-- type TypeResult = string?
Outputs a union of the inputted type but with nil singleton types removed.
Name | Type | Description |
---|---|---|
input | any | The type to make non-nilable. |
type TypeResult = NonNilable<string?>
-- type TypeResult = string
Outputs true if the inputted type is nilable.
Name | Type | Description |
---|---|---|
input | any | The type to test to see if it is nilable. |
type TypeResult = IsNonNilable<string?>
-- type TypeResult = true
Outputs true if the inputted type is non-nilable.
Name | Type | Description |
---|---|---|
input | any | The type to test to see if it is non-nilable. |
type TypeResult = IsNonNilable<string?>
-- type TypeResult = false
Table Types
Outputs the inputted table but only with specified properties.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to pick properties from. |
toPick | any | The union of types or a singleton/primitive to be picked. |
type TypeResult = TablePick<{
name: string,
age: number,
[string | number]: "fooBar"
}, "name" | string>
--[[
type TypeResult = {
[string]: "fooBar",
name: string
}
]]
Outputs the inputted table but only with properties whos keys extends the specified types.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to pick properties from. |
toPick | any | The union of types or a singleton/primitive to pick from. |
type TypeResult = TablePickExtends<{
name: string,
age: number,
[string | number]: "fooBar"
}, string>
--[[
type TypeResult = {
[string]: "fooBar",
age: number,
name: nil
}
]]
Outputs the inputted table but with specified properties omitted.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to omit properties from. |
toPick | any | The union of types (or a singleton/primitive) to be omitted. |
type TypeResult = TableOmit<{
name: string,
age: number
}, "age">
--[[
type TypeResult = {
name: string
}
]]
Outputs the inputted table but without components which extends the specified types.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to omit properties from. |
toPick | any | The union of types (or a singleton/primitive) to omit from. |
type TypeResult = TableOmitExtends<{
name: string,
age: number
}, string>
--[[
type TypeResult = { }
]]
Flattens intersections of tables into one consolidated type.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to flatten. |
type TypeResult = TableFlatten<
{ Name: string, Age: number } &
{ Kind: "Employee" }
>
--[[
type TypeResult = {
Age: number,
Kind: "Employee",
Name: string
}
]]
Removes duplicate components from unions and intersections inside of table keys, values and indexers.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to clean. |
type TypeResult = TableClean<{ Name: string | string, Age: number }>
-- type TypeResult = { Age: number, Name: string }
Outputs true if the two inputted tables are identical.
Name | Type | Description |
---|---|---|
inputA | { [any]: any } | The first table to compare. |
inputB | { [any]: any } | The second table to compare. |
type TypeResult = TableEquals<{ Name: "Bob" }, { Name: "Bob" }>
-- type TypeResult = true
Outputs a table of properties which only appear in inputA, and which only appear in inputB.
Name | Type | Description |
---|---|---|
inputA | { [any]: any } | The first table. |
inputB | { [any]: any } | The second table. |
type TypeResult = TableDiff<
{ Hello: "World", Foo: "Bar" },
{ Hello: "World", Baz: "Biz" }
>
--[[
type TypeResult = {
Baz: "Biz",
Foo: "Bar"
}
]]
Outputs a table of properties which only appear in both inputA and inputB.
Name | Type | Description |
---|---|---|
inputA | { [any]: any } | The first table. |
inputB | { [any]: any } | The second table. |
type TypeResult = TableOverlap<
{ Hello: "World", Foo: "Bar" },
{ Hello: "World", Baz: "Biz" }
>
-- type TypeResult = { Hello: "World" }
Returns a union of the two inputted tables, where keys of the first table are added to the second table (if not already) but with a falsy value and vice virsa.
Name | Type | Description |
---|---|---|
inputA | { [any]: any } | The first table. |
inputB | { [any]: any } | The second table. |
type TypeResult = Either<
{ Success: true, Data: string },
{ Success: false }
>
--[[
type TypeResult = {
Data: (false | never)?,
Success: false
} | {
Data: string,
Success: true
}
]]
Outputs a type where another type needs to have at least one of the properties from the input type in order to satify it.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table type input. |
type TypeResult = TableAtLeast<{
hello: "world", foo: "bar", baz: "bix"
}>
--[[
type TypeResult = {
baz: "bix",
foo: "bar"?,
hello: "world"?
} | {
baz: "bix"?,
foo: "bar",
hello: "world"?
} | {
baz: "bix"?,
foo: "bar"?,
hello: "world"
}
]]
Makes all of the properties in a table optional.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to make partial. |
type TypeResult = Partial<{ hello: "world", foo: "bar" }>
--[[
type TypeResult = {
foo: "bar"?,
hello: "world"?
}
]]
Picks all the partial keys from a table.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to pick partial keys from. |
type TypeResult = PickPartial<{
hello: "world"?, foo: "bar", baz: "bax"?
}>
-- type TypeResult = { baz: "bax"?, hello: "world"? }
Makes all of the properties in a table required.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to make required. |
type TypeResult = Required<{ hello: "world"?, foo: "bar"? }>
-- type TypeResult = { foo: "bar", hello: "world" }
Picks all the required keys from a table.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to pick required keys from. |
type TypeResult = PickPartial<{
hello: "world"?, foo: "bar", baz: "bax"?
}>
-- type TypeResult = { foo: "bar" }
Makes all of the properties in a table read only.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to make read only. |
type TypeResult = ReadOnly<{ hello: "world", foo: "bar" }>
--[[
type TypeResult = {
read foo: "bar",
read hello: "world"
}
]]
Makes all of the properties of a table readable and writable (mutable).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to make mutable. |
type TypeResult = ReadWrite<{ read hello: "world", read foo: "bar" }>
--[[
type TypeResult = {
foo: "bar",
hello: "world"
}
]]
Outputs all values of a table as a union of types (or a singleton/primitive).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to get values of. |
type TypeResult = ValueOf<{ hello: "world", foo: "bar" }>
-- type TypeResult = "bar" | "world"
Removes the indexer from a table type.
Name | Type | Description |
---|---|---|
tble | { [any]: any } | The table to remove the indexer from. |
type TypeResult = TableRemoveIndexer<{ hello: "world", [number]: number }>
-- type TypeResult = { hello: "world" }
Sets the indexer for a table type.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to set the indexer for. |
keyType | any | The key type for the new indexer. |
value | any | The value for the new indexer. |
type TypeResult = TableSetIndexer<{ foo: "bar", [number]: number }, string, "hello world">
--[[
type TypeResult = {
[string]: "hello world",
foo: "bar"
}
]]
Converts all string literal keys in a table to be camel case (camelCase).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to convert to camel case. |
type TypeResult = TableToCamel<{ Name: string, Age: number }>
--[[
type TypeResult = {
age: number,
name: string
}
]]
Converts all string literal keys in a table to be pascal case (PascalCase).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to convert to pascal case. |
type TypeResult = TableToPascal<{ name: string, age: number }>
--[[
type TypeResult = {
Age: number,
Name: string
}
]]
Converts all string literal keys in a table to be upper case (PascalCase).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to convert to upper case. |
type TypeResult = TableToUpper<{ name: string, age: number }>
--[[
type TypeResult = {
AGE: number,
NAME: string
}
]]
Converts all string literal keys in a table to be lower case (lowercase).
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to convert to lower case. |
type TypeResult = TableToLower<{ NaMe: string, AgE: number }>
--[[
type TypeResult = {
age: number,
name: string
}
]]
Returns true if the table is empty.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to test emptiness for. |
type TypeResult = TableIsEmpty<{}>
-- type TypeResult = true
Gets the metatable for a table.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to get the metatable for. |
type TypeResult = GetMetatable<SetMetatable<{ foo: "bar" }, { get: () -> string }>>
-- type TypeResult = { get: () -> string }
Sets the metatable for a table.
Name | Type | Description |
---|---|---|
input | { [any]: any } | The table to set the metatable for. |
metatable | { [any]: any } | The metatable to set. |
type MyMetatable = { get: () -> string, __index: MyMetatable }
type TypeResult = SetMetatable<{ foo: "bar" }, MyMetatable>
--[[
{
@metatable t1,
{
foo: "bar"
}
} where t1 = {
__index: t1,
get: () -> string
}
]]
Union Types
Outputs the inputted union or singleton/primitive but only with specified components.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to pick components from. |
toPick | any | The union of types (or a singleton/primitive) to be picked. |
type TypeResult = UnionPick<
"hello" | string | "world",
"world"
>
-- type TypeResult = "world"
Outputs the inputted union or singleton/primitive but only with components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to pick components from. |
toPick | any | The union of types (or a singleton/primitive) to pick from. |
type TypeResult = UnionPickExtends<
"hello" | false | string | "world" | boolean,
false | string
>
-- type TypeResult = "hello" | "world" | false | string
Outputs the inputted union or singleton/primitive but with specified components omitted.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to omit properties from. |
toOmit | any | The union of types (or a singleton/primitive) to be omitted. |
type TypeResult = UnionOmit<
"hello" | string | "world",
"world"
>
-- type TypeResult = "hello" | string
Outputs the inputted union or singleton/primitive but without components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to omit properties from. |
toOmit | any | The union of types (or a singleton/primitive) to omit from. |
type TypeResult = UnionOmitExtends<
"hello" | false | string | "world" | boolean,
false | string
>
-- type TypeResult = "hello" | string | "world"
Removes duplicate types from a union (or a singleton/primitive).
Name | Type | Description |
---|---|---|
input | any | The union of types (or a singleton/primitive) to be cleaned. |
type TypeResult = UnionClean<"hello" | string | "world" | string | "foo" | "hello">
-- type TypeResult = "foo" | "hello" | "world" | string
Recursively flattens nested unions into one union, semantics are preserved.
Name | Type | Description |
---|---|---|
input | any | The union of types (or a singleton/primitive) to be flattened. |
type TypeResult = UnionFlatten<"foo" | ("hello" | ("world" | "lol"))>
-- type TypeResult = "foo" | "hello" | "lol" | "world"
Outputs true if the two inputted unions (or a singleton/primitive) are identical.
Name | Type | Description |
---|---|---|
inputA | any | The first union to compare. |
inputB | any | The second union to compare. |
type TypeResult = UnionEquals<"hello" | "lol", "hello" | "kek">
-- type TypeResult = false
Outputs a union of components which only appear in inputA
, and which only appear in inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first union. |
inputB | any | The second union. |
type TypeResult = UnionDiff<"hello" | "foo", "hello" | "bar">
-- type TypeResult = "bar" | "foo"
Outputs a union of components which only appear in both inputA
and inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first union. |
inputB | any | The second union. |
type TypeResult = UnionOverlap<"hello" | "foo", "hello" | "bar">
-- type TypeResult = "hello"
Intersection Types
Outputs the inputted intersection or singleton/primitive but only with specified components.
Name | Type | Description |
---|---|---|
input | any | The intersection or singleton/primitive to pick components from. |
toPick | any | The union of types (or a singleton/primitive) to be picked. |
type TypeResult = IntersectionPick<
"hello" & string & "world",
"world"
>
-- type TypeResult = "world"
Outputs the inputted intersection or singleton/primitive but with components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to pick properties from. |
toPick | any | The union of types (or a singleton/primitive) to pick from. |
type TypeResult = IntersectionPickExtends<
"hello" & false & string & "world" & boolean,
false & string
>
-- type TypeResult = "hello" & "world" & false & string
Outputs the inputted intersection or singleton/primitive but with specified components omitted.
Name | Type | Description |
---|---|---|
input | any | The intersection or singleton/primitive to omit properties from. |
toOmit | any | The intersection of types (or a singleton/primitive) to be omitted. |
type TypeResult = IntersectionOmit<
"hello" & string & "world",
"world"
>
-- type TypeResult = "hello" & string
Outputs the inputted intersection or singleton/primitive but without components which extends the specified types.
Name | Type | Description |
---|---|---|
input | any | The union or singleton/primitive to omit properties from. |
toOmit | any | The union of types (or a singleton/primitive) to omit from. |
type TypeResult = IntersectionOmitExtends<
"hello" & false & string & "world" & boolean,
false & string
>
-- type TypeResult = "hello" & string & "world"
Removes duplicate types from a intersection (or a singleton/primitive).
Name | Type | Description |
---|---|---|
input | any | The intersection of types (or a singleton/primitive) to be cleaned. |
type TypeResult = IntersectionClean<"hello" & string & "world" & string & "foo" & "hello">
-- type TypeResult = "foo" & "hello" & "world" & string
Recursively flattens nested intersections into one intersection, semantics are preserved.
Name | Type | Description |
---|---|---|
input | any | The intersection of types (or a singleton/primitive) to be flattened. |
type TypeResult = IntersectionFlatten<"foo" & ("hello" & ("world" & "lol"))>
-- type TypeResult = "foo" & "hello" & "lol" & "world"
Outputs true if the two inputted intersections (or a singleton/primitive) are identical.
Name | Type | Description |
---|---|---|
inputA | any | The first intersection to compare. |
inputB | any | The second intersection to compare. |
type TypeResult = IntersectionEquals<"hello" & "lol", "hello" & "kek">
-- type TypeResult = false
Outputs an intersection of components which only appear in inputA
, and which only appear in inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first intersection. |
inputB | any | The second intersection. |
type TypeResult = IntersectionDiff<"hello" & "foo", "hello" & "bar">
-- type TypeResult = "bar" & "foo"
Outputs an intersection of components which only appear in both inputA
and inputB
.
Name | Type | Description |
---|---|---|
inputA | any | The first intersection. |
inputB | any | The second intersection. |
type TypeResult = IntersectionOverlap<"hello" & "foo", "hello" & "bar">
-- type TypeResult = "hello"
Function Types
Removes duplicate types from a functions arguments and return types.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to be cleaned. |
type TypeResult = FunctionClean<(number, string | string | boolean) -> any | any>
-- type TypeResult = (number, boolean | string) -> any
Recursively flattens the arguments and parameters of a function so that intersections, unions and intersections of tables are flattened into one consolidated type.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to be flattened. |
type TypeResult = FunctionClean<(string | (number | boolean)) -> (any & (boolean & number))>
-- type TypeResult = (boolean | number | string) -> any & boolean & number
Outputs true if the two inputted functions have identical arguments and return types.
Name | Type | Description |
---|---|---|
inputA | (...any) -> ...any | The first function to compare. |
inputB | (...any) -> ...any | The second function to compare. |
type TypeResult = FunctionEquals<(string) -> number, (number) -> string>
-- type TypeResult = false
Outputs the arguments of a function.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to get arguments for. |
type TypeResult = Args<(number, string, boolean) -> any>
--[[
type TypeResult = {
1: number,
2: string,
3: boolean
}
]]
Sets the arguments for an existing function type.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to set arguments for. |
args | { [{number} ]: any, Tail: any } |
The new arguments for the function. |
type MyFunction = () -> { name: string, age: number }
type TypeResult = SetArgs<MyFunction, { ["1"]: string, Tail: any }>
--[[
type TypeResult = (string, ...any) -> {
age: number,
name: string
}
]]
Outputs the return types of a function.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to get return types for. |
type TypeResult = Returns<() -> (string, number)>
--[[
type TypeResult = {
1: string,
2: number
}
]]
Sets the return types for a function.
Name | Type | Description |
---|---|---|
input | (...any) -> ...any | The function to set return types for. |
returns | { [{number} ]: any, Tail: any } |
The new return types for the function. |
type MyFunction = (string) -> boolean
type TypeResult = SetReturns<MyFunction, { ["1"]: number }>
-- type TypeResult = (string) -> number
Builds a function type using a table for the arguments and the return types.
Name | Type | Description |
---|---|---|
args | { [{number} ]: any, Tail: any } |
The arguments for the function. |
returns | { [{number} ]: any, Tail: any } |
The new return types for the function. |
type TypeResult = Function<
{ ["1"]: string, ["2"]: number },
{ ["1"]: boolean, Tail: string }
>
-- type TypeResult = (string, number) -> (boolean, ...string)
String Types
Converts a string literal (or string literals within a union/intersection) to camel case (camelCase).
Name | Type | Description |
---|---|---|
input | string | The string to convert to camel case. |
type TypeResult = StringToCamel<"HelloWorld">
-- type TypeResult = "helloWorld"
Converts a string literal (or string literals within a union/intersection) to pascal case (PascalCase).
Name | Type | Description |
---|---|---|
input | string | The string to convert to pascal case. |
type TypeResult = StringToPascal<"helloWorld">
-- type TypeResult = "HelloWorld"
Converts a string literal (or string literals within a union/intersection) to lower case (lowercase).
Name | Type | Description |
---|---|---|
input | string | The string to convert to lower case. |
type TypeResult = StringToLower<"helloWorld">
-- type TypeResult = "helloworld"
Converts a string literal (or string literals within a union/intersection) to upper case (UPPERCASE).
Name | Type | Description |
---|---|---|
input | string | The string to convert to upper case. |
type TypeResult = StringToUpper<"helloWorld">
-- type TypeResult = "HELLOWORLD"
Replaces part(s) of a string literal (or string literals within a union/intersection) with another using a pattern.
Name | Type | Description |
---|---|---|
input | string | The string to replace in. |
replace | string | The string pattern to replace. |
replaceWith | string | The replacement string. |
type TypeResult = StringReplace<"wolf", "f$", "ves">
-- type TypeResult = "wolves"
Joins a table of strings together.
Name | Type | Description |
---|---|---|
input | { [{number} ]: string } |
The string table to join together. |
type TypeResult = StringJoin<{ ["1"]: "Hello", ["2"]: " world!" }>
-- type TypeResult = "Hello world!"
Splits a string literal (or string literals within a union/intersection) at every occurance of a specific string.
Name | Type | Description |
---|---|---|
input | string | The string to split. |
splitAt | string | The string to split at. |
type TypeResult = StringSplit<"Hello, world!", ",">
--[[
type TypeResult = {
1: "Hello",
2: " world!"
}
]]
Returns the character of a string at a specific index.
Name | Type | Description |
---|---|---|
input | string | The string table to join together. |
at | {number} |
The stringified position to get character at. |
type TypeResult = StringAt<"hello", "2">
-- type TypeResult = "e"
Gets the length of a string (returns as a stringified integer as luau doesn't currently support integer literals).
Name | Type | Description |
---|---|---|
input | string | The string to get the length of. |
type TypeResult = StringLength<"hello">
-- type TypeResult = "5"
Outputs true if the input string is of the required length.
Name | Type | Description |
---|---|---|
input | string | The string to test the length of. |
length | {number} |
The expected length of the string. |
type TypeResult = StringIsLength<"hello", "5">
-- type TypeResult = true
Outputs true if the string is empty.
Name | Type | Description |
---|---|---|
input | string | The string to test emptiness for. |
type TypeResult = StringIsEmpty<"">
-- type TypeResult = true
Returns true if the string is a string literal.
Name | Type | Description |
---|---|---|
input | string | The string to test to see if its a string literal. |
type TypeResult = StringIsLiteral<"Hello">
-- type TypeResult = true
Boolean Operation Types
If a truthy type is inputted then it outputs false
, and if a falsy type is inputted then it outputs true
.
Name | Type | Description |
---|---|---|
input | any | The union/singleton you wish to perform a Not operation on. |
type TypeResult = Not<true>
-- type TypeResult = false
If all types of the union (or singleton/primitive) are truthy then it outputs true
, but if at least one of the types of the (or singleton/primitive) are falsely then it outputs false
.
Name | Type | Description |
---|---|---|
input | any | The union/singleton you wish to perform an And operation on. |
type TypeResult = And<true | false>
-- type TypeResult = false
If at least one of the types of the union (or singleton/primitive) are truthy then it outputs true
, but if all of the types of the union (or singleton/primitive) are falsely then it outputs false
.
Name | Type | Description |
---|---|---|
input | any | The union/singleton you wish to perform an Or operation on. |
type TypeResult = Or<true | false>
-- type TypeResult = true
Logical Operation Types
Returns true if all of the input types extends at least one of the output types.
Name | Type | Description |
---|---|---|
input | any | The type to test. |
extends | any | The type to test if input extends. |
type CustomerSchema = { name: string, age: number, kind: "Customer" }
type TypeResult = Extends<{ name: "Bob", age: number, kind: "Employee" }, CustomerSchema>
-- This does not extend `CustomerSchema` as `kind` is not the string literal `"Customer"`.
-- type TypeResult = false
Returns true if input
has the same type or subtype (via vanilla luau subtyping) to compareTo
.
Name | Type | Description |
---|---|---|
input | any | The type to compare. |
compareTo | any | The type to compare to. |
type CustomerSchema = { name: string, age: number, kind: "Customer" }
type TypeResult = Compare<{ name: "Bob", age: number, kind: "Employee" }, CustomerSchema>
-- type TypeResult = true
If input
is a truthy type then it outputs ifTruthy
, if else then it outputs ifFalsy
.
Name | Type | Description |
---|---|---|
input | any | The type for the condition. |
ifTruthy | any | The type to output if input is truthy. |
ifFalsy | any | The type to output if input is falsy. |
type TypeResult = Condition<
StringIsLiteral<"Bob">,
"Is String Literal",
"Is Not String Literal"
>
-- type TypeResult = "Is String Literal"
Miscellaneous Types
Throws a type error if the first type does not equal the second.
Name | Type | Description |
---|---|---|
expect | any | The type to be compared. |
toBe | any | The type you want to compare expect to. |
type TypeResult = Expect<true, false>
-- TypeError: 'Expect' type function errored at runtime: [string "Expect"]:872: expection error!
Returns the inputted type but with unions and intersections turned into arrays so they can be inspected better.
Name | Type | Description |
---|---|---|
input | any | The type to be inspected. |
type TypeResult = Expect<true, false>
-- TypeError: 'Expect' type function errored at runtime: [string "Expect"]:872: expection error!