> ## Documentation Index
> Fetch the complete documentation index at: https://gluals.arnux.net/llms.txt
> Use this file to discover all available pages before exploring further.

# @type

> Declare the type of a variable or expression.

## 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

```lua theme={null}
---@type TypeExpression
---@type (instance) TypeExpression
---@type (definition) TypeExpression
```

***

## Basic usage

```lua theme={null}
---@type string
local userName = "Player1"

---@type number
local score = 0

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

***

## Union types

```lua theme={null}
---@type string | number
local id = getUserId()  -- could be either
```

***

## Optionals (nil-able)

```lua theme={null}
---@type Player | nil
local maybePly = ents.GetByIndex(1)
```

or using the shorthand `?`:

```lua theme={null}
---@type Player?
local maybePly = ents.GetByIndex(1)
```

***

## Table types

```lua theme={null}
---@type table<string, number>
local scores = {}

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

***

## Array types

```lua theme={null}
---@type Entity[]
local allEnts = ents.GetAll()
```

***

## Function types

```lua theme={null}
---@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:

```lua theme={null}
---@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:

```lua theme={null}
---@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
