Skip to main content

Introduction

Type syntax nodes represent type annotations used in Luau and typed Lua dialects. These nodes are only used when the AcceptTypedLua 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 from TypeSyntax, 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 identifier
  • TypeArgumentList - Optional generic type arguments
Key properties (CompositeTypeNameSyntax):
  • Base - The base type name
  • DotToken - The . separator
  • IdentifierToken - The member type name
  • TypeArgumentList - Optional generic type arguments
// Simple type: number
var numberType = SyntaxFactory.SimpleTypeName(
    SyntaxFactory.Identifier("number")
);

// Generic type: Array<string>
var arrayType = SyntaxFactory.SimpleTypeName(
    SyntaxFactory.Identifier("Array"),
    SyntaxFactory.TypeArgumentList(
        SyntaxFactory.SeparatedList<TypeSyntax>(new[] {
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string"))
        })
    )
);

// Composite type: module.Type
var compositeType = SyntaxFactory.CompositeTypeName(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("module")),
    SyntaxFactory.Identifier("Type")
);

LiteralTypeSyntax

Represents singleton literal types. Syntax: "literal", true, false Key properties:
  • Token - The literal token
var stringLiteralType = SyntaxFactory.LiteralType(
    SyntaxFactory.Literal("success")
);
// Produces: "success"

var trueLiteralType = SyntaxFactory.LiteralType(
    SyntaxFactory.Token(SyntaxKind.TrueKeyword)
);
// Produces: true

Function Types

FunctionTypeSyntax

Represents function type signatures. Syntax: (param1: Type1, param2: Type2) -> ReturnType Key properties:
  • TypeParameterList - Optional generic type parameters <T, U>
  • OpenParenthesisToken - The ( token
  • Parameters - List of function type parameters
  • CloseParenthesisToken - The ) token
  • MinusGreaterThanToken - The -> token
  • ReturnType - The return type
var funcType = SyntaxFactory.FunctionType(
    null,  // no generic params
    SyntaxFactory.SeparatedList(new[] {
        SyntaxFactory.FunctionTypeParameter(
            SyntaxFactory.Identifier("x"),
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
        ),
        SyntaxFactory.FunctionTypeParameter(
            SyntaxFactory.Identifier("y"),
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
        )
    }),
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
);
// Produces: (x: number, y: number) -> number

FunctionTypeParameterSyntax

Represents a parameter in a function type. Syntax: name: Type Key properties:
  • Identifier - Parameter name (optional)
  • ColonToken - The : token
  • Type - Parameter type

Table Types

TableTypeSyntax

Represents table type literals. Syntax: { field1: Type1, field2: Type2, [KeyType]: ValueType } Key properties:
  • OpenBraceToken - The { token
  • Elements - List of table type elements (properties and indexers)
  • CloseBraceToken - The } token
var tableType = SyntaxFactory.TableType(
    SyntaxFactory.SeparatedList<TableTypeElementSyntax>(new[] {
        SyntaxFactory.TableTypeProperty(
            SyntaxFactory.Identifier("x"),
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
        ),
        SyntaxFactory.TableTypeProperty(
            SyntaxFactory.Identifier("y"),
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
        ),
        SyntaxFactory.TableTypeIndexer(
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string")),
            SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("any"))
        )
    })
);
// Produces: { x: number, y: number, [string]: any }

TableTypePropertySyntax

Represents a named property in a table type. Syntax: name: Type Key properties:
  • Identifier - Property name
  • ColonToken - The : token
  • ValueType - Property type

TableTypeIndexerSyntax

Represents an indexer in a table type. Syntax: [KeyType]: ValueType Key properties:
  • OpenBracketToken - The [ token
  • IndexType - The key type
  • CloseBracketToken - The ] token
  • ColonToken - The : token
  • ValueType - The value type

ArrayTypeSyntax

Represents array type shorthand. Syntax: {Type} (equivalent to {[number]: Type}) Key properties:
  • OpenBraceToken - The { token
  • Type - The element type
  • CloseBraceToken - The } token
var arrayType = SyntaxFactory.ArrayType(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string"))
);
// Produces: {string}

Composite Types

UnionTypeSyntax

Represents union types (or-types). Syntax: Type1 | Type2 | Type3 Key properties:
  • Left - Left type
  • PipeToken - The | token
  • Right - Right type (can be another union)
var unionType = SyntaxFactory.UnionType(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string")),
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
);
// Produces: string | number

// For three or more types, nest them:
var multiUnion = SyntaxFactory.UnionType(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string")),
    SyntaxFactory.UnionType(
        SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number")),
        SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("boolean"))
    )
);
// Produces: string | number | boolean

IntersectionTypeSyntax

Represents intersection types (and-types). Syntax: Type1 & Type2 Key properties:
  • Left - Left type
  • AmpersandToken - The & token
  • Right - Right type (can be another intersection)
var intersectionType = SyntaxFactory.IntersectionType(
    SyntaxFactory.TableType(
        SyntaxFactory.SeparatedList(new[] {
            SyntaxFactory.TableTypeProperty(
                SyntaxFactory.Identifier("name"),
                SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string"))
            )
        })
    ),
    SyntaxFactory.TableType(
        SyntaxFactory.SeparatedList(new[] {
            SyntaxFactory.TableTypeProperty(
                SyntaxFactory.Identifier("age"),
                SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
            )
        })
    )
);
// Produces: { name: string } & { age: number }

NilableTypeSyntax

Represents optional/nilable types. Syntax: Type? (equivalent to Type | nil) Key properties:
  • Type - The base type
  • QuestionToken - The ? token
var nilableType = SyntaxFactory.NilableType(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string"))
);
// Produces: string?

Special Types

TypeofTypeSyntax

Represents typeof expressions. Syntax: typeof(expression) Key properties:
  • TypeofKeyword - The typeof keyword
  • OpenParenthesisToken - The ( token
  • Expression - The expression to get the type of
  • CloseParenthesisToken - The ) token
var typeofType = SyntaxFactory.TypeofType(
    SyntaxFactory.IdentifierName("myVariable")
);
// Produces: typeof(myVariable)

ParenthesizedTypeSyntax

Represents a type wrapped in parentheses. Syntax: (Type) Key properties:
  • OpenParenthesisToken - The ( token
  • Type - The inner type
  • CloseParenthesisToken - The ) token
var parenType = SyntaxFactory.ParenthesizedType(
    SyntaxFactory.FunctionType(
        null,
        SyntaxFactory.SeparatedList<FunctionTypeParameterSyntax>(),
        SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("void"))
    )
);
// Produces: (() -> void)

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 ( token
  • Types - List of types
  • CloseParenthesisToken - The ) token

VariadicTypePackSyntax

Represents a variadic type pack. Syntax: ...Type Key properties:
  • DotDotDotToken - The ... token
  • Type - The element type

GenericTypePackSyntax

Represents a generic type pack parameter. Syntax: T... Key properties:
  • Identifier - The type parameter name
  • DotDotDotToken - The ... token

Type Bindings and Annotations

TypeBindingSyntax

Represents a type annotation (colon and type). Syntax: : Type Key properties:
  • ColonToken - The : token
  • Type - The type
var typeBinding = SyntaxFactory.TypeBinding(
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
);
// Produces: : number

TypeParameterListSyntax

Represents generic type parameters. Syntax: <T, U, V> Key properties:
  • LessThanToken - The < token
  • Names - List of type parameter names
  • GreaterThanToken - The > token
var typeParams = SyntaxFactory.TypeParameterList(
    SyntaxFactory.SeparatedList(new[] {
        SyntaxFactory.TypeParameter(SyntaxFactory.Identifier("T")),
        SyntaxFactory.TypeParameter(SyntaxFactory.Identifier("U"))
    })
);
// Produces: <T, U>

TypeParameterSyntax

Represents a single type parameter. Syntax: T or T... or T = DefaultType Key properties:
  • Identifier - Parameter name
  • DotDotDotToken - Optional ... for variadic type parameters
  • EqualsType - Optional default type

TypeArgumentListSyntax

Represents generic type arguments. Syntax: <Type1, Type2> Key properties:
  • LessThanToken - The < token
  • Arguments - List of type arguments
  • GreaterThanToken - The > token

Using Type Annotations

In Variable Declarations

var localDecl = SyntaxFactory.LocalVariableDeclarationStatement(
    SyntaxFactory.SeparatedList(new[] {
        SyntaxFactory.LocalDeclarationName(
            SyntaxFactory.IdentifierName("count"),
            null,  // no attribute
            SyntaxFactory.TypeBinding(
                SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
            )
        )
    }),
    SyntaxFactory.EqualsValuesClause(
        SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
            SyntaxFactory.LiteralExpression(
                SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(0)
            )
        })
    )
);
// Produces: local count: number = 0

In Function Parameters

var funcDecl = SyntaxFactory.FunctionDeclarationStatement(
    SyntaxFactory.SimpleFunctionName(SyntaxFactory.Identifier("greet")),
    SyntaxFactory.ParameterList(
        SyntaxFactory.SeparatedList<ParameterSyntax>(new[] {
            SyntaxFactory.NamedParameter(
                SyntaxFactory.Identifier("name"),
                SyntaxFactory.TypeBinding(
                    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("string"))
                )
            )
        })
    ),
    SyntaxFactory.StatementList()
);
// Produces: function greet(name: string) end

In Function Return Types

var funcDecl = SyntaxFactory.FunctionDeclarationStatement(
    SyntaxFactory.SimpleFunctionName(SyntaxFactory.Identifier("getId")),
    null,  // no type parameters
    SyntaxFactory.ParameterList(
        SyntaxFactory.SeparatedList<ParameterSyntax>()
    ),
    SyntaxFactory.TypeBinding(
        SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
    ),
    SyntaxFactory.StatementList(
        SyntaxFactory.ReturnStatement(
            SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                SyntaxFactory.LiteralExpression(
                    SyntaxKind.NumericalLiteralExpression,
                    SyntaxFactory.Literal(42)
                )
            })
        )
    )
);
// Produces: function getId(): number return 42 end

See Also

Build docs developers (and LLMs) love