Type Function API Reference
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.
- If the given type is a union type,
nilwill be added unconditionally as a component. - Otherwise, the result will be a union of the given type and the
niltype.
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" | "class" | "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" | "class" | "thread" | "buffer")
Returns true if self has the argument as its tag.
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.
keyis expected to be a string singleton type, naming the property.valuewill be set as both theread typeandwrite typeof the property.- If
valueisnil, the property is removed.
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.
keyis expected to be a string singleton type, naming the property.valuewill be set as theread type, thewrite typewill be unchanged.- If
keyis not already present, only aread typewill be set, making the property read-only. - If
valueisnil, the property is removed.
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.
keyis expected to be a string singleton type, naming the property.valuewill be set as thewrite type, theread typewill be unchanged.- If
keyis not already present, only awrite typewill be set, making the property write-only. - If
valueisnil, the property is removed.
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.
Class type instance
classtype:properties(): { [type]: { read: type?, write: type? } }
Returns the properties of the class with their respective read and write types.
classtype:readparent(): type?
Returns the type of reading this class’ parent, or returns nil if the parent class doesn’t exist.
classtype:writeparent(): type?
Returns the type for writing to this class’ parent, or returns nil if the parent class doesn’t exist.
classtype:metatable(): type?
Returns the class’ metatable, or nil if it doesn’t exist.
classtype:indexer(): { index: type, readresult: type, writeresult: type }?
Returns the class’ indexer, or nil if it doesn’t exist.
classtype:readindexer(): { index: type, result: type }?
Returns result type of reading from the class via indexing, or nil if it doesn’t exist.
classtype:writeindexer(): { index: type, result: type }?
Returns the type for writing to the class via indexing, or nil if it doesn’t exist.