# guardDefined()

## `guardDefined()`

Guards the value to be defined, not [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined).

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

```typescript
const guardDefined = <Type, Payload extends object = object>(
  value: Defined<Type>,
  callback?: ResultCallback<Defined<Type>, Payload>,
  payload?: Payload
): value is Defined<Type> => isDefined(value, callback, payload);
```

{% endcode %}

### Generic type variables

#### <mark style="color:green;">**`Type`**</mark>

A generic type variable `Type` indicates captured type of the given [`value`](#value-defined-less-than-type-greater-than) via the [return type](#return-type) and the [`value`](https://type.angular-package.dev/type/resultcallback#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-defined-less-than-type-greater-than-payload-greater-than) function [`ResultCallback`](https://type.angular-package.dev/type/resultcallback) 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`](https://type.angular-package.dev/type/resultcallback#payload-payload) of the supplied [`callback`](#callback-resultcallback-less-than-defined-less-than-type-greater-than-payload-greater-than) function and [`payload`](#payload-payload) optional parameter of the [`guardDefined()`](#guarddefined) function from which it captures its value.

### Parameters

#### `value: Defined<Type>`

The value of generic type [`Defined<Type>`](https://type.angular-package.dev/type/defined), [never](https://www.typescriptlang.org/docs/handbook/basic-types.html#never) undefined type captured from itself to guard against [`undefined`](https://developer.mozilla.org/en-US/docs/Glossary/undefined).

#### `callback?: ResultCallback<Defined<Type>, Payload>`

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

#### `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`](https://type.angular-package.dev/type/resultcallback#payload-payload) of the given [`callback`](#callback-resultcallback-less-than-defined-less-than-type-greater-than-payload-greater-than) function.

### Return type

#### `value is Defined<Type>`

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-defined-less-than-type-greater-than) is a generic type [`Defined`](https://type.angular-package.dev/type/defined) that takes a generic type variable [`Type`](#type) of value by default equal to the type captured from the supplied [`value`](#value-defined-less-than-type-greater-than) parameter excepts [`undefined`](https://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined) which changes to [`never`](https://www.typescriptlang.org/docs/handbook/basic-types.html#never).

### Returns

The **return value** is a [`boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) indicating whether the [`value`](#value-defined-less-than-type-greater-than) is defined.

## Example usage

```typescript
// Example usage.
import { guardDefined } from '@angular-package/type';

let letFirstName = 'my name';
guardDefined(letFirstName); // true; return type `value is string`

const firstName = 'my const name';
guardDefined(firstName); // true; return type `value is string`
```
