Type Function Library
The types library is used to create and transform types, and can only be used within type functions.
types library properties
Section titled “types library properties”types.anyThe any type.
types.unknownThe unknown type.
types.neverThe never type.
types.booleanThe boolean type.
types.bufferThe buffer type.
types.numberThe number type.
types.stringThe string type.
types.threadThe thread type.
types library functions
Section titled “types library functions”types.singleton(arg: string | boolean | nil): typeReturns the singleton type of the argument.
types.negationof(arg: type): typeReturns an immutable negation of the argument type.
types.optional(arg: type): typeReturns 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): typeReturns an immutable union of two or more arguments.
types.intersectionof(first: type, second: type, ...: type): typeReturns 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?): typeReturns 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}?): typeReturns a fresh, mutable function type, using the ordered parameters of head and the variadic tail of tail.
types.copy(arg: type): typeReturns a deep copy of the argument type.
types.generic(name: string?, ispack: boolean?): typeCreates a generic named name. If ispack is true, the result is a generic pack.
type instance
Section titled “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): booleanOverrides 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
Section titled “Singleton type instance”singletontype:value(): boolean | nil | "string"Returns the singleton’s actual value, like true for types.singleton(true).
Generic type instance
Section titled “Generic type instance”generictype:name(): string?Returns the name of the generic or nil if it has no name.
generictype:ispack(): booleanReturns true if the generic is a pack, or false otherwise.
Table type instance
Section titled “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
Section titled “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
Section titled “Negation type instance”type:inner(): typeReturns the type being negated.
Union type instance
Section titled “Union type instance”uniontype:components(): {type}Returns an array of the unioned types.
Intersection type instance
Section titled “Intersection type instance”intersectiontype:components()Returns an array of the intersected types.
Class type instance
Section titled “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.