# 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`](/type/type/anystring.md) 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`](/type/type/resultcallback.md#value-value) parameter of the provided [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function [`ResultCallback`](/type/type/resultcallback.md) 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`](/type/type/resultcallback.md#payload-payload) parameter type of the provided [`callback`](#callback-resultcallback-less-than-type-length-length-and-payload-greater-than) function [`ResultCallback`](/type/type/resultcallback.md) 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`](/type/type/resultcallback.md#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`](/type/type/anystring.md), 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`](/type/type/resultcallback.md) type with parameters, the [`value`](#value-type) that has been checked, the [`result`](/type/type/resultcallback.md#result-boolean) of this check, and [`payload`](/type/type/resultcallback.md#payload-payload) of generic type variable [`Payload`](#payloadextendsobject) with optional properties from the provided [`payload`](#payload-payload), to handle them before the [`result`](/type/type/resultcallback.md#result-boolean) return. By default, it uses [`resultCallback()`](/type/helper/resultcallback.md) 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`](/type/type/resultcallback.md#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`](/type/type/stringoflength.md) 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);
```


---

# 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://docs.angular-package.dev/type/guard/guardstringlength.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.
