Skip to main content

Overview

@type annotates a local variable, global, or table field with a specific type. Use it when GLuaLS cannot infer the type from code, such as external data or dynamic construction.

Syntax

---@type TypeExpression
---@type (instance) TypeExpression
---@type (definition) TypeExpression

Basic usage

---@type string
local userName = "Player1"

---@type number
local score = 0

---@type Player
local ply = GetSomePlayer()

Union types

---@type string | number
local id = getUserId()  -- could be either

Optionals (nil-able)

---@type Player | nil
local maybePly = ents.GetByIndex(1)
or using the shorthand ?:
---@type Player?
local maybePly = ents.GetByIndex(1)

Table types

---@type table<string, number>
local scores = {}

---@type { name: string, health: number }
local data = GetPlayerData()

Array types

---@type Entity[]
local allEnts = ents.GetAll()

Function types

---@type fun(ply: Player): boolean
local predicate = IsAdmin

(instance) modifier

When you use (instance), GLuaLS keeps fields added after this annotation on only that variable. It does not modify the global class definition:
---@type (instance) Panel
local myPanel = vgui.Create("DPanel")

function myPanel:Refresh() end  -- Only exists on myPanel, not all Panels

(definition) modifier

When you use (definition), GLuaLS treats fields added to the variable as global additions to the class:
---@type (definition) Entity
local EntityDef = Entity

EntityDef.MyCustomMethod = function(self) end  -- Added to Entity globally

When to use @type vs @class

  • Use @class when defining a new reusable class that other code will reference by name
  • Use @type when annotating the type of a specific local variable