# guardKey()

## `guardKey()`

Guards the value to be one of [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [`number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), or [`symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) type.

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

```typescript
const guardKey = <Key extends PropertyKey, Payload extends object>(
  value: Key,
  callback?: ResultCallback<Key, Payload>,
  payload?: Payload
): value is Key => isKey(value, callback, payload);
```

{% endcode %}

{% embed url="<https://github.com/angular-package/type/blob/main/src/guard/lib/guard-key.func.ts>" %}

### Generic type variables

#### <mark style="color:green;">**`Key`**</mark>**`extends`**<mark style="color:green;">**`PropertyKey`**</mark>

A generic type variable `Key` constrained by `PropertyKey` indicates captured type of the given [`value`](#value-key) via the [return type](#return-type) and the [`value`](https://type.angular-package.dev/type-draft/type/resultcallback#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-key-payload-greater-than) function [`ResultCallback`](https://type.angular-package.dev/type-draft/type/resultcallback) type.

#### <mark style="color:green;">**`Payload`**</mark>**`extends`**<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-draft/type/resultcallback#payload-payload) of the supplied [`callback`](#callback-resultcallback-less-than-key-payload-greater-than) function and [`payload`](#payload-payload) optional parameter of the [`guardKey()`](#guardkey) function from which it captures its value.

### Parameters

#### `value: Key`

The value of generic type variable [`Key`](#keyextendspropertykey) to guard.

#### `callback?: ResultCallback<Key, Payload>`

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

### Return type

#### `value is Key`

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-key) is generic type variable [`Key`](#keyextendspropertykey).

### 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-key) is a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [`number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), or [`symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol).

## Example usage

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

guardKey('string key'); // true, value is PropertyKey
guardKey(27); // true, value is PropertyKey
guardKey(Symbol('string key')); // true, value is PropertyKey
guardKey(Symbol(27)); // true, value is PropertyKey

guardKey(new String('string key') as any); // false, value is PropertyKey
guardKey(new Number(27) as any); // false, value is PropertyKey
```
