# guardStringLength()

## `guardStringLength()`

Guards the value to be [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type or [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) instance of a specified [`length`](#length-length).

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

```typescript
const guardStringLength = <
  Type extends AnyString,
  Length extends number,
  Payload extends object = object
>(
  value: Type,
  length: Length,
  callback?: ResultCallback<Type, { length: Length } & Payload>,
  payload?: Payload
): value is StringOfLength<Length, Length, Type> =>
  isStringLength(value, length, callback, payload);
```

{% endcode %}

### Generic type variables

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

A generic type variable `Obj` constrained by [`AnyString`](https://docs.angular-package.dev/type/type/anystring) indicates captured [`string`](https://www.typescriptlang.org/docs/handbook/basic-types.html#string) type of the given [`value`](#value-type) via the [return type](#return-type) and the [`value`](https://docs.angular-package.dev/type/type/resultcallback#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type.

#### <mark style="color:green;">**`Length`**</mark>**`extends`**<mark style="color:green;">**`number`**</mark>

A generic type variable `Length` constrained by the [`number`](https://www.typescriptlang.org/docs/handbook/basic-types.html#number) type, by default of value captured from the supplied [`length`](#length-length) indicates the [`payload`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) parameter type of the provided [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type and the **length** of the provided [`value`](#value-type) via the [return type](#return-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://docs.angular-package.dev/type/type/resultcallback#payload-payload) of the supplied [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function and [`payload`](#payload-payload) optional parameter of the [`guardStringLength()`](#guardstringlength) function from which it captures its value.

### Parameters

#### `value: Type`

The value of a generic type variable [`Type`](#typeextendsanystring) constrained by [`AnyString`](https://docs.angular-package.dev/type/type/anystring), by default of the type captured from itself to guard.

#### `length: Length`

The **length** of generic type variable [`Length`](#lengthextendsnumber) of a given [`value`](#value-type).

#### `callback?: ResultCallback<Type, { length: Length } & Payload>`

The optional callback [`function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) of [`ResultCallback`](https://docs.angular-package.dev/type/type/resultcallback) type with parameters, the [`value`](#value-type) that has been checked, the [`result`](https://docs.angular-package.dev/type/type/resultcallback#result-boolean) of this check, and [`payload`](https://docs.angular-package.dev/type/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://docs.angular-package.dev/type/type/resultcallback#result-boolean) return. By default, it uses [`resultCallback()`](https://docs.angular-package.dev/type/helper/resultcallback) function.

{% hint style="info" %}
The **`payload`** parameter of given `callback` function consists of the **`length`** property of the given [`length`](#length-length), and it can't be overwritten by the given [`payload`](#payload-payload) parameter of the main 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`](https://docs.angular-package.dev/type/type/resultcallback#payload-payload) of the given [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function.

### Return type

#### `value is StringOfLength<Length, Length, 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-type) is a generic type [`StringOfLength`](https://docs.angular-package.dev/type/type/stringoflength) that takes generic type variable `Min` and `Max`(from the provided [`length`](#length-length) parameter) as the **length** of the supplied [`value`](#value-type), and [`Type`](#typeextendsanystring) as the type of the supplied [`value`](#value-type).

### 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-type) is a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type or an instance of [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) of a specified [`length`](#length-length).

## Example usage

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

// true; return type `value is StringOfLength<11, 11, "not my name">`
guardStringLength('not my name', 11);
// false; return type `value is StringOfLength<10, 10, "not my name">` 
guardStringLength('not my name', 10);
// false; return type `value is StringOfLength<12, 12, "not my name">`
guardStringLength('not my name', 12);
```
