Skip to content

Latest commit

 

History

History
1678 lines (1129 loc) · 35.2 KB

README.md

File metadata and controls

1678 lines (1129 loc) · 35.2 KB

Typeforge

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.)

Documentation below

Core Types

Pick

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"

PickExtends

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

Omit

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"

OmitExtends

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

Clean

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
}
]]

Flatten

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"
}
]]

Equals

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

Overlap

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"

Diff

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"

ToCamel

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 }

ToPascal

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"

ToUpper

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"

ToLower

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"
}
]]

IsEmpty

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

Nilable

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?

NonNilable

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

IsNilable

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

IsNonNilable

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

TablePick

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
}
]]

TablePickExtends

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
}
]]

TableOmit

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
}
]]

TableOmitExtends

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 = {  }
]]

TableFlatten

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
}
]]

TableClean

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 }

TableEquals

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

TableDiff

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"
}
]]

TableOverlap

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" }

Either

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
}
]]

TableAtLeast

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"
}
]]

Partial

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"?
}
]]

PickPartial

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"? }

Required

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" }

PickRequired

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" }

ReadOnly

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"
}
]]

ReadWrite

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"
}
]]

ValueOf

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"

RemoveIndexer

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" }

SetIndexer

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"
}
]]

TableToCamel

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
}
]]

TableToPascal

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
}
]]

TableToUpper

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
}
]]

TableToLower

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
}
]]

TableIsEmpty

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

GetMetatable

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 }

SetMetatable

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

UnionPick

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"

UnionPickExtends

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

UnionOmit

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

UnionOmitExtends

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"

UnionClean

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

UnionFlatten

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"

UnionEquals

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

UnionDiff

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"

UnionOverlap

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

IntersectionPick

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"

IntersectionPickExtends

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

IntersectionOmit

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

IntersectionOmitExtends

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"

IntersectionClean

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

IntersectionFlatten

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"

IntersectionEquals

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

IntersectionDiff

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"

IntersectionOverlap

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

FunctionClean

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

FunctionFlatten

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

FunctionEquals

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

Args

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
}
]]

SetArgs

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
}
]]

Returns

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
}
]]

SetReturns

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

Function

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

StringToCamel

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"

StringToPascal

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"

StringToLower

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"

StringToUpper

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"

StringReplace

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"

StringJoin

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!"

StringSplit

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!"
}
]]

StringAt

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"

StringLength

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"

StringIsLength

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

StringIsEmpty

Outputs true if the string is empty.

Name Type Description
input string The string to test emptiness for.
type TypeResult = StringIsEmpty<"">

-- type TypeResult = true

StringIsLiteral

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

Not

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

And

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

Or

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

Extends

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

Compare

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

Condition

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

Expect

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!

Inspect

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!