Skip to main content

Overview

@enum marks a local table as an enum type. Table values become valid enum members. GLuaLS can report invalid enum values with enum-value-mismatch when you enable that diagnostic.

Syntax

---@enum EnumName
---@enum (key) EnumName

Basic enum

---@enum HTTPStatus
local HTTPStatus = {
    OK = 200,
    NOT_FOUND = 404,
    INTERNAL_ERROR = 500,
    BAD_REQUEST = 400,
    UNAUTHORIZED = 401,
}

---@param status HTTPStatus
function HandleResponse(status)
    if status == HTTPStatus.OK then
        -- ...
    end
end

String enums

---@enum GameState
local GameState = {
    WAITING = "waiting",
    ACTIVE = "active",
    ENDED = "ended",
}

(key) modifier

Use (key) when the table keys are the enum values:
---@enum (key) Direction
local Direction = {
    north = true,
    south = true,
    east = true,
    west = true,
}

---@type Direction
local d = "north" -- ✅
local d2 = "up"   -- ❌ not a Direction

Enum vs. @alias

Both @enum and @alias with ---| can describe a fixed set of values. Use:
  • @enum when your addon has a Lua table of named constants that code reads at runtime
  • @alias with ---| when a string parameter accepts a fixed set of text values

Common patterns in GMod

---@enum DAMAGE
local DAMAGE = {
    GENERIC = 1,
    CRUSH = 2,
    BULLET = 4,
    SLASH = 8,
    BURN = 16,
}
GMod’s built-in enums, such as DAMAGE, TEAM, and MOVETYPE, come from the downloaded annotations. You do not need to redeclare them.