Skip to main content

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:
If you access an optional field without a nil check, GLuaLS reports 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.

Index-type fields

Use [keyType] syntax to define all keys of a type at once:

Function fields

Fields can use function types:
Use this when code outside the Lua file adds the function, such as C++. If Lua defines the function, annotate the function itself to avoid duplicate definitions.

Tips and best practices

  • Place @field annotations directly after their @class declaration.
  • GLuaLS disables the inject-field diagnostic 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.