
Lua annotations provide a powerful way to document code and enable better type checking and auto-completion in modern development environments. This guide will walk you through everything you need to know about Lua annotations, from basic concepts to advanced usage.
Lua annotations are special comments that start with three dashes (---) instead of the standard two dashes (--). These annotations provide additional information about your code that can be used by development tools for:
Lua annotations always begin with three dashes followed by an @ symbol and the annotation type:
---@type string
local name = "John"
---@param x number The value to square
---@return number The squared value
local function square(x)
return x * x
endUse @type to specify the type of a variable:
---@type string
local username = "johndoe"
---@type number[]
local scores = {98, 95, 92}Document function parameters:
---@param name string The user's name
---@param age number The user's age
---@param isAdmin boolean Whether the user is an administrator
local function createUser(name, age, isAdmin)
-- Function implementation
endSpecify function return types:
---@return boolean success Whether the operation succeeded
---@return string? error Error message if operation failed
local function saveData()
-- Function implementation
endDefine classes and their fields:
---@class Player
---@field name string
---@field health number
---@field level integer
local Player = {}
function Player:new()
local instance = {
name = "",
health = 100,
level = 1
}
return setmetatable(instance, { __index = Player })
endCreate custom type aliases for reusability:
---@alias UserID string
---@alias Status "active"|"inactive"|"banned"
---@param id UserID
---@param status Status
local function updateUserStatus(id, status)
-- Function implementation
endDefine generic types for flexible functions:
---@generic T
---@param list T[]
---@return T?
local function getFirstElement(list)
return list[1]
endSpecify multiple possible types:
---@type string|number
local identifier
---@param value string|number|boolean
local function process(value)
-- Function implementation
endBe Consistent
Provide Clear Descriptions
---@param userId string The unique identifier of the user
---@param options table Configuration options for the action
---@return boolean success Whether the action was successful
---@return string? error Error message if the action failed
local function performAction(userId, options)
-- Function implementation
end---@param callback? function Optional callback function
---@return string? result May be nil if operation fails
local function doSomething(callback)
-- Function implementation
end---@alias Point {x: number, y: number}
---@alias Size {width: number, height: number}
---@alias Rectangle {position: Point, size: Size}
---@param rect Rectangle
local function drawRectangle(rect)
-- Function implementation
endArray Type Declaration
-- Incorrect
---@type number[]array
-- Correct
---@type number[]
Optional Parameters
-- Incorrect
---@param name string|nil
-- Correct
---@param name? string
Function Types
-- Incorrect
---@type function(string)
-- Correct
---@type fun(param: string): void
Major IDEs and editors that support Lua annotations:
Keep It Brief
---@param x number Input value
-- Better than long, multi-line descriptions for simple parameters
Use Consistent Terminology
---@param callback function Called when operation completes
---@param handler function Called when event occurs
-- Use consistent terms for similar concepts
Document Edge Cases
---@param divisor number Must be non-zero
---@return number quotient
---@return string? error Returns error message if divisor is zero
Lua annotations are a powerful tool for improving code quality and maintainability. By providing clear type information and documentation, they help catch errors early and make code easier to understand and maintain. Start small with basic annotations and gradually incorporate more advanced features as you become comfortable with the syntax.
Remember that the goal of annotations is to make your code more maintainable and easier to understand. Use them consistently and thoughtfully, and they'll become an invaluable part of your Lua development workflow.
Happy coding!