Overview
@field declares a class property. You can add an access level, a ? for nilable fields, and a description. Annotate fields you use often so GLuaLS can give better types and autocomplete.
Syntax
Basic fields
Place@field annotations immediately after a @class line:
Optional fields
Add? after the field name to mark it as nil sometimes:
need-check-nil when you enable strict nil checking.
Access control
Access modifiers do not change runtime behavior. GLuaLS checks them and can report diagnostics when code uses private or protected fields from the wrong place. Treat them as type and style rules, not runtime access control.
| Modifier | Description |
|---|---|
public | Accessible everywhere (default) |
private | Only accessible within class methods |
protected | Only accessible in the class and subclasses |
package | Only accessible within the same file/package |
Index-type fields
Use[keyType] syntax to define all keys of a type at once:
Function fields
Fields can use function types:Tips and best practices
- Place
@fieldannotations directly after their@classdeclaration. - GLuaLS disables the
inject-fielddiagnostic by default. Classes act as(partial), so you can define fields anywhere in the file. Fields are optional, but they improve type checking and autocomplete for the class.