@param annotation documents function parameters with type information, enabling IDE autocomplete, type checking, and inline documentation.
Syntax
?- Marks the parameter as optional...- Indicates variadic parameters- Supports union types, generics, and complex type expressions
IDE Benefits
Basic Parameter Types
---@param name string Username
---@param age number Age in years
---@param active boolean Account status
function createUser(name, age, active)
return {
name = name,
age = age,
active = active
}
end
---@param name string Username
---@param age? number Age (optional, defaults to 18)
---@param email? string Email address (optional)
function registerUser(name, age, email)
age = age or 18
return {
name = name,
age = age,
email = email
}
end
-- Usage
local user1 = registerUser("John") -- age and email are optional
local user2 = registerUser("Jane", 25) -- email is optional
local user3 = registerUser("Bob", 30, "[email protected]")
Advanced Parameter Types
Variadic Parameters
Document functions that accept variable arguments:Function Parameters
Define callback signatures with parameter and return types:Complex Object Parameters
Define inline table structures:Generic Parameters
Create type-safe generic functions:Method Parameters
Explicit self Parameter
Colon Syntax (self auto-inferred)
Before/After Comparison
Without @param
- No autocomplete
- Type errors go undetected
- No documentation
- Hard to understand API
With @param
- Full autocomplete support
- Type validation
- Inline documentation
- Clear API contract
Complete Example
Best Practices
Always document public function parameters
Always document public function parameters
Every public API function should have parameter annotations for discoverability and type safety.
Use optional markers (?) appropriately
Use optional markers (?) appropriately
Mark parameters as optional only when they truly have default values or can be nil.
Provide meaningful descriptions
Provide meaningful descriptions
Descriptions help users understand the parameter’s purpose beyond just its type.
Use union types for flexible parameters
Use union types for flexible parameters
When a parameter can accept multiple types, use union types (e.g.,
string | number) instead of any.Define inline table structures for complex objects
Define inline table structures for complex objects
Inline table definitions provide better autocomplete than just using
table.