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.

Default values

Add = value after the type when something else fills the field in later, such as a base class or a setup function:
You can leave a field with a default out of a table, and GLuaLS does not report missing-fields. When you read it, the type stays number — not number? — so you do not need a nil check.
Use = value when the field is always there by the time you read it. Use ? when it can still be nil at read time. Picking ? for this case forces nil checks everywhere you read the field.
Defaults accept numbers, strings, true, false, nil, and a name or call such as Color(255, 255, 255):

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.