# guardInstance()

## `guardInstance()`

Guards the value to be an instance of the given [`constructor`](#constructor-constructor-less-than-obj-greater-than).

{% code title="guard-instance.func.ts" %}

```typescript
const guardInstance = <
  Obj extends object,
  Payload extends object = object
>(
  value: Obj,
  constructor: Constructor<Obj>,
  callback?: ResultCallback<Obj, { ctor: typeof constructor } & Payload>,
  payload?: Payload
): value is Obj => isInstance(value, constructor, callback, payload);
```

{% endcode %}

Code on [**GitHub**](https://github.com/angular-package/type/blob/5.0.x/src/guard/lib/guard-instance.func.ts).

### Generic type variables

#### <mark style="color:green;">**`Obj`**</mark>**`extends`**<mark style="color:green;">**`object`**</mark>

A generic type variable `Obj` constrained by [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) indicates captured [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) type of the given [`value`](#value-obj) via the [return type](#return-type) and the [`value`](/type/resultcallback.md#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-obj-ctor-typeof-constructor-and-payload-greater-than) function [`ResultCallback`](/type/resultcallback.md) type.

#### <mark style="color:green;">**`Payload`**</mark>**`extends`**<mark style="color:green;">**`object`**</mark>**`=`**<mark style="color:green;">**`object`**</mark>

The `Payload` generic type variable constrained by [`object`](https://www.typescriptlang.org/docs/handbook/basic-types.html#object) indicates the type of optional parameter [`payload`](/type/resultcallback.md#payload-payload) of the supplied [`callback`](#callback-resultcallback-less-than-obj-ctor-typeof-constructor-and-payload-greater-than) function and [`payload`](#payload-payload) optional parameter of the [`guardInstance()`](#guardinstance) function from which it captures its value.

### Parameters

#### `value: Obj`

An [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) of a generic type variable [`Obj`](#typeextendsanyboolean) to guard and be compared with an instance of a given [`constructor`](#constructor-constructor-less-than-obj-greater-than).

#### `constructor: Constructor<Obj>`

A [`class`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class) or [`function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) that specifies the type of the [`Constructor`](/type/constructor.md).

#### `callback?: ResultCallback<Obj, { ctor: typeof constructor } & Payload>`

The optional callback [`function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) of [`ResultCallback`](/type/resultcallback.md) type with parameters, the [`value`](#value-obj) that has been checked, the [`result`](/type/resultcallback.md#result-boolean) of this check, and [`payload`](/type/resultcallback.md#payload-payload) of generic type variable [`Payload`](#payloadextendsobject-object) with optional properties from the provided [`payload`](#payload-payload), to handle them before the [`result`](/type/resultcallback.md#result-boolean) return. By default, it uses [`resultCallback()`](/helper/resultcallback.md) function.

{% hint style="info" %}
The [`payload`](/type/resultcallback.md#payload-payload) parameter of the [`callback`](#callback-resultcallback-less-than-obj-ctor-typeof-constructor-and-payload-greater-than) function consists of the **`ctor`** property under which is set given [`constructor`](#constructor-constructor-less-than-obj-greater-than), and it can't be overwritten by the given [`payload`](#payload-payload) parameter of the core function.
{% endhint %}

#### `payload?: Payload`

An optional [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) of the generic type variable [`Payload`](#payloadextendsobject-object) is assigned to the [`payload`](/type/resultcallback.md#payload-payload) of the given [`callback`](#callback-resultcallback-less-than-obj-ctor-typeof-constructor-and-payload-greater-than) function.

### Return type

#### `value is Obj`

The **return type** is a [`boolean`](https://www.typescriptlang.org/docs/handbook/basic-types.html#boolean) as the result of its statement indicating the [`value`](#value-obj) is a generic type variable [`Obj`](#objextendsobject) by default of the type captured from the provided [`value`](#value-obj).

### Returns

The **return value** is a [`boolean`](https://www.typescriptlang.org/docs/handbook/basic-types.html#boolean) indicating whether the [`value`](#value-obj) is an instance of a given [`constructor`](#constructor-constructor-less-than-obj-greater-than).

## Example usage

```typescript
// Usage example.
import { guardInstance } from '@angular-package/type';

// Person interface.
interface Person {
  firstName?: string;
  surname?: string;
  age?: number;
  sex?: 'male' | 'female';
}

// Class constructor.
class Persons implements Person {
  firstName = '';
  surname = '';
  age = 15;
}

// Function person constructor.
function personFunctionConstructor(this: Person, ...args: any[]): Person {
  if (args) {
    this.firstName = args[0];
    this.surname = args[1];
    this.age = args[2];
    this.sex = args[3];
  }
  return this;
}

const personInstance: Person = new (personFunctionConstructor as any)('First name', 'Sur name', 27);
const personsInstance: Persons = new Persons();

guardInstance(personInstance, personFunctionConstructor as any); // true
guardInstance(personsInstance, Persons); // true
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://type.angular-package.dev/guard/guardinstance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
