Introduction
Type syntax nodes represent type annotations used in Luau and typed Lua dialects. These nodes are only used when theAcceptTypedLua syntax option is enabled.
Type annotations can appear in:
- Variable declarations:
local x: number = 5 - Function parameters:
function add(a: number, b: number) - Function return types:
function getName(): string - Type aliases:
type Point = { x: number, y: number } - Generic type parameters:
function map<T>(arr: {T}): {T}
TypeSyntax Base Class
All type nodes inherit fromTypeSyntax, which inherits from LuaSyntaxNode.
Simple Types
SimpleTypeNameSyntax / CompositeTypeNameSyntax
Represents named types, potentially with generic arguments. Syntax:number, string, Array<T>, Map<K, V>, module.Type
Key properties (SimpleTypeNameSyntax):
IdentifierToken- The type name identifierTypeArgumentList- Optional generic type arguments
Base- The base type nameDotToken- The.separatorIdentifierToken- The member type nameTypeArgumentList- Optional generic type arguments
LiteralTypeSyntax
Represents singleton literal types. Syntax:"literal", true, false
Key properties:
Token- The literal token
Function Types
FunctionTypeSyntax
Represents function type signatures. Syntax:(param1: Type1, param2: Type2) -> ReturnType
Key properties:
TypeParameterList- Optional generic type parameters<T, U>OpenParenthesisToken- The(tokenParameters- List of function type parametersCloseParenthesisToken- The)tokenMinusGreaterThanToken- The->tokenReturnType- The return type
FunctionTypeParameterSyntax
Represents a parameter in a function type. Syntax:name: Type
Key properties:
Identifier- Parameter name (optional)ColonToken- The:tokenType- Parameter type
Table Types
TableTypeSyntax
Represents table type literals. Syntax:{ field1: Type1, field2: Type2, [KeyType]: ValueType }
Key properties:
OpenBraceToken- The{tokenElements- List of table type elements (properties and indexers)CloseBraceToken- The}token
TableTypePropertySyntax
Represents a named property in a table type. Syntax:name: Type
Key properties:
Identifier- Property nameColonToken- The:tokenValueType- Property type
TableTypeIndexerSyntax
Represents an indexer in a table type. Syntax:[KeyType]: ValueType
Key properties:
OpenBracketToken- The[tokenIndexType- The key typeCloseBracketToken- The]tokenColonToken- The:tokenValueType- The value type
ArrayTypeSyntax
Represents array type shorthand. Syntax:{Type} (equivalent to {[number]: Type})
Key properties:
OpenBraceToken- The{tokenType- The element typeCloseBraceToken- The}token
Composite Types
UnionTypeSyntax
Represents union types (or-types). Syntax:Type1 | Type2 | Type3
Key properties:
Left- Left typePipeToken- The|tokenRight- Right type (can be another union)
IntersectionTypeSyntax
Represents intersection types (and-types). Syntax:Type1 & Type2
Key properties:
Left- Left typeAmpersandToken- The&tokenRight- Right type (can be another intersection)
NilableTypeSyntax
Represents optional/nilable types. Syntax:Type? (equivalent to Type | nil)
Key properties:
Type- The base typeQuestionToken- The?token
Special Types
TypeofTypeSyntax
Represents typeof expressions. Syntax:typeof(expression)
Key properties:
TypeofKeyword- ThetypeofkeywordOpenParenthesisToken- The(tokenExpression- The expression to get the type ofCloseParenthesisToken- The)token
ParenthesizedTypeSyntax
Represents a type wrapped in parentheses. Syntax:(Type)
Key properties:
OpenParenthesisToken- The(tokenType- The inner typeCloseParenthesisToken- The)token
Type Packs
Type packs represent multiple types, used in return types and variadic parameters.TypePackSyntax
Represents a type pack (tuple). Syntax:(Type1, Type2, Type3)
Key properties:
OpenParenthesisToken- The(tokenTypes- List of typesCloseParenthesisToken- The)token
VariadicTypePackSyntax
Represents a variadic type pack. Syntax:...Type
Key properties:
DotDotDotToken- The...tokenType- The element type
GenericTypePackSyntax
Represents a generic type pack parameter. Syntax:T...
Key properties:
Identifier- The type parameter nameDotDotDotToken- The...token
Type Bindings and Annotations
TypeBindingSyntax
Represents a type annotation (colon and type). Syntax:: Type
Key properties:
ColonToken- The:tokenType- The type
TypeParameterListSyntax
Represents generic type parameters. Syntax:<T, U, V>
Key properties:
LessThanToken- The<tokenNames- List of type parameter namesGreaterThanToken- The>token
TypeParameterSyntax
Represents a single type parameter. Syntax:T or T... or T = DefaultType
Key properties:
Identifier- Parameter nameDotDotDotToken- Optional...for variadic type parametersEqualsType- Optional default type
TypeArgumentListSyntax
Represents generic type arguments. Syntax:<Type1, Type2>
Key properties:
LessThanToken- The<tokenArguments- List of type argumentsGreaterThanToken- The>token
Using Type Annotations
In Variable Declarations
In Function Parameters
In Function Return Types
See Also
- Overview - Syntax node hierarchy
- Statements - Statement node types
- Expressions - Expression node types
- Syntax Options - Enabling typed Lua support