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

# @self_guard

> Mark a method that proves the object it is called on is valid.

> [!NOTE]
>
> This annotation is intended for complex cases and is primarily designed to be used by the built-in annotations.

## Overview

`self_guard` marks a colon method such as `Entity:IsValid` that returns `true` only when the object it is called on is valid. After `if ent:IsValid() then`, GLuaLS treats `ent` as valid.

The official annotations already mark `IsValid` on entities, panels and other Garry's Mod objects.

***

## Syntax

```lua theme={null}
---@return TypeGuard<T>
---@[self_guard("domain")]
```

Put it after the `@return` or `@return_cast` line.

***

## Example

```lua theme={null}
---@return TypeGuard<Entity>
---@[self_guard("gmod.entity")]
function Entity:IsValid() end
```

```lua theme={null}
---@type Entity?
local ent

if ent:IsValid() then
    ent:GetClass() -- ent is valid here
end
```

***

## Keep it narrow

It applies only to the annotated method. Only use it on a method that really checks validity, or it hides real `nil` and `NULL` bugs.

***

## See also

* [`@valid_guard`](https://gluals.arnux.net/annotations/valid-guard): a function that proves its argument is valid
* [`@self_call_valid`](https://gluals.arnux.net/annotations/self-call-valid): a callback where a `self` method always returns a valid value


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.