Skip to main content

Overview

Type annotations are available when using the Luau feature flag. These structures represent type information in Luau code.

TypeInfo Enum

Any type, such as string, boolean?, number | boolean, etc.
pub enum TypeInfo {
    /// A shorthand type annotating the structure of an array: { number }
    Array {
        /// The braces (`{}`) containing the type info.
        braces: ContainedSpan,
        /// The access modifier of the array, `read` in `{ read number }`.
        access: Option<TokenReference>,
        /// The type info for the values in the Array
        type_info: Box<TypeInfo>,
    },
    
    /// A standalone type, such as `string` or `Foo`.
    Basic(TokenReference),
    
    /// A singleton string type, such as `"hello"`
    String(TokenReference),
    
    /// A singleton boolean type, such as `true`
    Boolean(TokenReference),
    
    /// A callback type, such as `(string, number) => boolean`.
    Callback {
        /// Optional generics provided for the arguments, such as in `<T>(T) -> string`
        generics: Option<GenericDeclaration>,
        /// The parentheses for the arguments.
        parentheses: ContainedSpan,
        /// The argument types: `(string, number)`.
        arguments: Punctuated<TypeArgument>,
        /// The "thin arrow" (`->`) in between the arguments and the return type.
        arrow: TokenReference,
        /// The return type: `boolean`.
        return_type: Box<TypeInfo>,
    },
    
    /// A type using generics, such as `map<number, string>`.
    Generic {
        /// The type that has generics: `map`.
        base: TokenReference,
        /// The arrows (`<>`) containing the type parameters.
        arrows: ContainedSpan,
        /// The type parameters: `number, string`.
        generics: Punctuated<TypeInfo>,
    },
    
    /// A generic pack: `T...`.
    /// Note, these are only available as return types, when annotating a vararg (`...`) in a function parameter, or as a generic type argument.
    GenericPack {
        /// The name of the type that is generic: `T`.
        name: TokenReference,
        /// The ellipsis: `...`.
        ellipsis: TokenReference,
    },
    
    /// An intersection type, such as `string & number`.
    Intersection(TypeIntersection),
    
    /// A type coming from a module, such as `module.Foo`
    Module {
        /// The module the type is coming from: `module`.
        module: TokenReference,
        /// The punctuation (`.`) to index the module.
        punctuation: TokenReference,
        /// The indexed type info: `Foo`.
        type_info: Box<IndexedTypeInfo>,
    },
    
    /// An optional type, such as `string?`.
    Optional {
        /// The type that is optional: `string`.
        base: Box<TypeInfo>,
        /// The question mark: `?`.
        question_mark: TokenReference,
    },
    
    /// A type annotating the structure of a table: { foo: number, bar: string }
    Table {
        /// The braces (`{}`) containing the fields.
        braces: ContainedSpan,
        /// The fields: `foo: number, bar: string`.
        fields: Punctuated<TypeField>,
    },
    
    /// A type in the form of `typeof(foo)`.
    Typeof {
        /// The token `typeof`.
        typeof_token: TokenReference,
        /// The parentheses used to contain the expression.
        parentheses: ContainedSpan,
        /// The inner expression: `foo`.
        inner: Box<Expression>,
    },
    
    /// A tuple expression: `(string, number)`.
    Tuple {
        /// The parentheses used to contain the types
        parentheses: ContainedSpan,
        /// The types: `(string, number)`.
        types: Punctuated<TypeInfo>,
    },
    
    /// A union type, such as `string | number`.
    Union(TypeUnion),
    
    /// A variadic type: `...number`.
    Variadic {
        /// The ellipsis: `...`.
        ellipsis: TokenReference,
        /// The type that is variadic: `number`.
        type_info: Box<TypeInfo>,
    },
    
    /// A variadic type pack: `...T` in `Function<...T>`
    VariadicPack {
        /// The ellipsis: `...`
        ellipsis: TokenReference,
        /// The name of the type that is variadic: `T`
        name: TokenReference,
    },
}

TypeUnion

A union type, such as string | number.
pub struct TypeUnion {
    leading: Option<TokenReference>,
    types: Punctuated<TypeInfo>,
}

Methods

pub fn new(leading: Option<TokenReference>, types: Punctuated<TypeInfo>) -> Self
pub fn leading(&self) -> Option<&TokenReference>
The leading pipe, if one is present: |.
pub fn types(&self) -> &Punctuated<TypeInfo>
The types being unioned: string | number.
pub fn with_types(self, types: Punctuated<TypeInfo>) -> Self
pub fn with_leading(self, leading: Option<TokenReference>) -> Self

TypeIntersection

An intersection type, such as string & number.
pub struct TypeIntersection {
    leading: Option<TokenReference>,
    types: Punctuated<TypeInfo>,
}

Methods

pub fn new(leading: Option<TokenReference>, types: Punctuated<TypeInfo>) -> Self
pub fn leading(&self) -> Option<&TokenReference>
The leading ampersand, if one is present: &.
pub fn types(&self) -> &Punctuated<TypeInfo>
The types being intersected: string & number.
pub fn with_types(self, types: Punctuated<TypeInfo>) -> Self
pub fn with_leading(self, leading: Option<TokenReference>) -> Self

IndexedTypeInfo

A subset of TypeInfo that consists of items which can only be used as an index, such as Foo and Foo<Bar>.
pub enum IndexedTypeInfo {
    /// A standalone type, such as `string` or `Foo`.
    Basic(TokenReference),
    
    /// A type using generics, such as `map<number, string>`.
    Generic {
        /// The type that has generics: `map`.
        base: TokenReference,
        /// The arrows (`<>`) containing the type parameters.
        arrows: ContainedSpan,
        /// The type parameters: `number, string`.
        generics: Punctuated<TypeInfo>,
    },
}

TypeField

A type field used within table types. The foo: number in { foo: number }.
pub struct TypeField {
    access: Option<TokenReference>,
    key: TypeFieldKey,
    colon: TokenReference,
    value: TypeInfo,
}

Methods

pub fn new(key: TypeFieldKey, value: TypeInfo) -> Self
pub fn access(&self) -> Option<&TokenReference>
The access modifier of the field, read in read foo: number.
pub fn key(&self) -> &TypeFieldKey
The key of the field, foo in foo: number.
pub fn colon_token(&self) -> &TokenReference
The colon in between the key name and the value type.
pub fn value(&self) -> &TypeInfo
The type for the field, number in foo: number.
pub fn with_key(self, key: TypeFieldKey) -> Self
pub fn with_colon_token(self, colon_token: TokenReference) -> Self
pub fn with_access(self, access: Option<TokenReference>) -> Self
pub fn with_value(self, value: TypeInfo) -> Self

TypeFieldKey

A key in a TypeField. Can either be a name or an index signature.
pub enum TypeFieldKey {
    /// A name, such as `foo`.
    Name(TokenReference),
    
    /// An index signature, such as `[number]`.
    IndexSignature {
        /// The brackets (`[]`) used to contain the type.
        brackets: ContainedSpan,
        /// The type for the index signature, `number` in `[number]`.
        inner: TypeInfo,
    },
}

TypeAssertion

A type assertion using ::, such as :: number.
pub struct TypeAssertion {
    assertion_op: TokenReference,
    cast_to: TypeInfo,
}

Methods

pub fn new(cast_to: TypeInfo) -> Self
pub fn assertion_op(&self) -> &TokenReference
The token ::.
pub fn cast_to(&self) -> &TypeInfo
The type to cast the expression into, number in :: number.
pub fn with_assertion_op(self, assertion_op: TokenReference) -> Self
pub fn with_cast_to(self, cast_to: TypeInfo) -> Self

TypeDeclaration

A type declaration, such as type Meters = number.
pub struct TypeDeclaration {
    type_token: TokenReference,
    base: TokenReference,
    generics: Option<GenericDeclaration>,
    equal_token: TokenReference,
    declare_as: TypeInfo,
}

Methods

pub fn new(type_name: TokenReference, type_definition: TypeInfo) -> Self
pub fn type_token(&self) -> &TokenReference
The token type.
pub fn type_name(&self) -> &TokenReference
The name of the type, Meters in type Meters = number.
pub fn generics(&self) -> Option<&GenericDeclaration>
The generics of the type, if there are any. <T> in type Foo<T> = T.
pub fn equal_token(&self) -> &TokenReference
The = token in between the type name and the definition.
pub fn type_definition(&self) -> &TypeInfo
The definition of the type, number in type Meters = number.
pub fn with_type_token(self, type_token: TokenReference) -> Self
pub fn with_type_name(self, type_name: TokenReference) -> Self
pub fn with_generics(self, generics: Option<GenericDeclaration>) -> Self
pub fn with_equal_token(self, equal_token: TokenReference) -> Self
pub fn with_type_definition(self, type_definition: TypeInfo) -> Self

FunctionBody

A function body, everything except function x in function x(a, b, c) call() end.
pub struct FunctionBody {
    #[cfg(feature = "luau")]
    generics: Option<GenericDeclaration>,
    parameters_parentheses: ContainedSpan,
    parameters: Punctuated<Parameter>,
    #[cfg(feature = "luau")]
    type_specifiers: Vec<Option<TypeSpecifier>>,
    #[cfg(feature = "luau")]
    return_type: Option<TypeSpecifier>,
    block: Block,
    end_token: TokenReference,
}

Methods

pub fn new() -> Self
pub fn parameters_parentheses(&self) -> &ContainedSpan
pub fn parameters(&self) -> &Punctuated<Parameter>
pub fn block(&self) -> &Block
pub fn end_token(&self) -> &TokenReference
#[cfg(feature = "luau")]
pub fn generics(&self) -> Option<&GenericDeclaration>
The generics declared for the function body. The <T, U> part of function x<T, U>() end.
#[cfg(feature = "luau")]
pub fn type_specifiers(&self) -> impl Iterator<Item = Option<&TypeSpecifier>>
The type specifiers of the variables, in the order that they were assigned. (foo: number, bar, baz: boolean) returns an iterator containing: Some(TypeSpecifier(number)), None, Some(TypeSpecifier(boolean)).
#[cfg(feature = "luau")]
pub fn return_type(&self) -> Option<&TypeSpecifier>
The return type of the function, if one exists.
pub fn with_parameters_parentheses(self, parameters_parentheses: ContainedSpan) -> Self
pub fn with_parameters(self, parameters: Punctuated<Parameter>) -> Self
#[cfg(feature = "luau")]
pub fn with_generics(self, generics: Option<GenericDeclaration>) -> Self
#[cfg(feature = "luau")]
pub fn with_type_specifiers(self, type_specifiers: Vec<Option<TypeSpecifier>>) -> Self
#[cfg(feature = "luau")]
pub fn with_return_type(self, return_type: Option<TypeSpecifier>) -> Self
pub fn with_block(self, block: Block) -> Self
pub fn with_end_token(self, end_token: TokenReference) -> Self

Parameter Enum

A parameter in a function declaration.
pub enum Parameter {
    /// The `...` vararg syntax, such as `function x(...)`
    Ellipsis(TokenReference),
    
    /// A name parameter, such as `function x(a, b, c)`
    Name(TokenReference),
}

Example Usage

use full_moon::ast::types::*;

// Parse Luau code with type annotations
let ast = full_moon::parse("local x: number = 5").unwrap();

// Access type information from the AST
if let Some(local_assignment) = ast.nodes().stmts().next() {
    // Process type specifiers
}

Build docs developers (and LLMs) love