7 min read

Source: Luau.org · MIT · View source Imported 2026-10-03. Formatting adapted for this site.

Type Function Library

The types library is used to create and transform types, and can only be used within type functions.

types library properties

types.any

The any type.

types.unknown

The unknown type.

types.never

The never type.

types.boolean

The boolean type.

types.buffer

The buffer type.

types.number

The number type.

types.string

The string type.

types.thread

The thread type.

types library functions

types.singleton(arg: string | boolean | nil): type

Returns the singleton type of the argument.

types.negationof(arg: type): type

Returns an immutable negation of the argument type.

types.optional(arg: type): type

Returns a version of the given type that is now optional.

types.unionof(first: type, second: type, ...: type): type

Returns an immutable union of two or more arguments.

types.intersectionof(first: type, second: type, ...: type): type

Returns an immutable intersection of two or more arguments.

types.newtable(props: { [type]: type | { read: type?, write: type? } }?, indexer: { index: type, readresult: type, writeresult: type? }?, metatable: type?): type

Returns a fresh, mutable table type. Property keys must be string singleton types. The table's metatable is set if one is provided.

types.newfunction(parameters: { head: {type}?, tail: type? }, returns: { head: {type}?, tail: type? }?, generics: {type}?): type

Returns a fresh, mutable function type, using the ordered parameters of head and the variadic tail of tail.

types.copy(arg: type): type

Returns a deep copy of the argument type.

types.generic(name: string?, ispack: boolean?): type

Creates a generic named name. If ispack is true, the result is a generic pack.

type instance

type instances can have extra properties and methods described in subsections depending on its tag.

type.tag: "nil" | "unknown" | "never" | "any" | "boolean" | "number" | "string" | "singleton" | "negation" | "union" | "intersection" | "table" | "function" | "extern" | "thread" | "buffer"

An immutable property holding the type's tag.

__eq(arg: type): boolean

Overrides the == operator to return true if self is syntactically equal to arg. This excludes semantically equivalent types, true | false is unequal to boolean.

type:is(arg: "nil" | "unknown" | "never" | "any" | "boolean" | "number" | "string" | "singleton" | "negation" | "union" | "intersection" | "table" | "function" | "extern" | "thread" | "buffer")

Returns true if self has the argument as its tag.

type:issubtypeof(super: type): boolean

Returns true if self is a subtype of the provided super type argument.

type function isString(ty)
    return types.singleton(ty:issubtypeof(types.string))
end

local x: isString<string>  --> `true`
local y: isString<"hello"> --> `true`
local z: isString<false>   --> `false`

Singleton type instance

singletontype:value(): boolean | nil | "string"

Returns the singleton's actual value, like true for types.singleton(true).

Generic type instance

generictype:name(): string?

Returns the name of the generic or nil if it has no name.

generictype:ispack(): boolean

Returns true if the generic is a pack, or false otherwise.

Table type instance

tabletype:setproperty(key: type, value: type?)

Sets the type of the property for the given key, using the same type for both reading from and writing to the table.

tabletype:setreadproperty(key: type, value: type?)

Sets the type for reading from the property named by key, leaving the type for writing this property as-is.

tabletype:setwriteproperty(key: type, value: type?)

Sets the type for writing to the property named by key, leaving the type for reading this property as-is.

tabletype:readproperty(key: type): type?

Returns the type used for reading values from this property, or nil if the property doesn't exist.

tabletype:writeproperty(key: type): type?

Returns the type used for writing values to this property, or nil if the property doesn't exist.

tabletype:properties(): { [type]: { read: type?, write: type? } }

Returns a table mapping property keys to their read and write types.

tabletype:setindexer(index: type, result: type)

Sets the table's indexer, using the same type for reads and writes.

tabletype:setreadindexer(index: type, result: type)

Sets the type resulting from reading from this table via indexing.

tabletype:setwriteindexer(index: type, result: type)

Sets the type for writing to this table via indexing.

tabletype:indexer(): { index: type, readresult: type, writeresult: type }

Returns the table's indexer as a table, or nil if it doesn't exist.

tabletype:readindexer(): { index: type, result: type }?

Returns the table's indexer using the result's read type, or nil if it doesn't exist.

tabletype:writeindexer()

Returns the table's indexer using the result's write type, or nil if it doesn't exist.

tabletype:setmetatable(arg: type)

Sets the table's metatable.

tabletype:metatable(): type?

Gets the table's metatable, or nil if it doesn't exist.

Function type instance

functiontype:setparameters(head: {type}?, tail: type?)

Sets the function's parameters, with the ordered parameters in head and the variadic tail in tail.

functiontype:parameters(): { head: {type}?, tail: type? }

Returns the function's parameters, with the ordered parameters in head and the variadic tail in tail.

functiontype:setreturns(head: {type}?, tail: type?)

Sets the function's return types, with the ordered parameters in head and the variadic tail in tail.

functiontype:returns(): { head: {type}?, tail: type? }

Returns the function's return types, with the ordered parameters in head and the variadic tail in tail.

functiontype:generics(): {type}

Returns an array of the function's generic types.

functiontype:setgenerics(generics: {type}?)

Sets the function's generic types.

Negation type instance

type:inner(): type

Returns the type being negated.

Union type instance

uniontype:components(): {type}

Returns an array of the unioned types.

Intersection type instance

intersectiontype:components()

Returns an array of the intersected types.

Extern type instance

externtype:properties(): { [type]: { read: type?, write: type? } }

Returns the properties of the extern type with their respective read and write types.

externtype:readparent(): type?

Returns the type of reading this extern type's parent, or returns nil if the parent doesn't exist.

externtype:writeparent(): type?

Returns the type for writing to this extern type's parent, or returns nil if the parent doesn't exist.

externtype:metatable(): type?

Returns the extern type's metatable, or nil if it doesn't exist.

externtype:indexer(): { index: type, readresult: type, writeresult: type }?

Returns the extern type's indexer, or nil if it doesn't exist.

externtype:readindexer(): { index: type, result: type }?

Returns the result type of reading from the extern type via indexing, or nil if it doesn't exist.

externtype:writeindexer(): { index: type, result: type }?

Returns the type for writing to the extern type via indexing, or nil if it doesn't exist.