PHPackages                             pinkary-project/type-guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pinkary-project/type-guard

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pinkary-project/type-guard
==========================

Type Guard module is part of the Pinkary Project, and allows you to \*\*narrow down the type\*\* of a variable to a more specific type.

v0.1.0(2y ago)198143.4k—0.2%25[1 issues](https://github.com/pinkary-project/type-guard/issues)[6 PRs](https://github.com/pinkary-project/type-guard/pulls)11MITPHPPHP ^8.2.0CI passing

Since Apr 3Pushed 1mo ago7 watchersCompare

[ Source](https://github.com/pinkary-project/type-guard)[ Packagist](https://packagist.org/packages/pinkary-project/type-guard)[ Fund](https://www.paypal.com/paypalme/enunomaduro)[ GitHub Sponsors](https://github.com/nunomaduro)[ RSS](/packages/pinkary-project-type-guard/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependencies (6)Versions (7)Used By (11)

 [![GitHub Workflow Status (master)](https://github.com/pinkary-project/type-guard/actions/workflows/tests.yml/badge.svg)](https://github.com/pinkary-project/type-guard/actions) [![Total Downloads](https://camo.githubusercontent.com/e90607153c5872ef4a11d9bcb5884a9571cb38655e76d0f4e8ec1bba35bd5692/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70696e6b6172792d70726f6a6563742f747970652d6775617264)](https://packagist.org/packages/pinkary-project/type-guard) [![Latest Version](https://camo.githubusercontent.com/0ee060fe583c0b1c2493c46c7613bb03aa02b6616c8ce47324c6a56fa56b6318/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70696e6b6172792d70726f6a6563742f747970652d6775617264)](https://packagist.org/packages/pinkary-project/type-guard) [![License](https://camo.githubusercontent.com/2e6c4cf08eee45af16fcfebbd6b38be079c48720a14c6f67ec569f88574d6074/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70696e6b6172792d70726f6a6563742f747970652d6775617264)](https://packagist.org/packages/pinkary-project/type-guard)

---

> This library is a **work in progress**. Please, do not use it in production.

Type Guard module is part of the [Pinkary Project](https://github.com/pinkary-project), and allows you to **narrow down the type** of a variable to a more specific type. Using the `type` function, you can perform specific checks to determine the type of an object and then use that object in a way that is **type-safe** according to the [PHPStan](https://phpstan.org/) and [Psalm](https://psalm.dev/) static analyzers.

Here is an example, where we use the `type` function to narrow down the type of a variable that previously had a `mixed` type:

```
function config(): mixed;

// At compile time, the type of $apiKey is `mixed`:
$apiKey = config('api_key');

// We instruct the static analyzer that $apiKey is a `string`:
$apiKey = type($apiKey)->asString();
```

Here is another example, where we use the `type` function to narrow down the type of a variable that previously could be `null`. In the process, zero type information is lost:

```
/** @var array|null $users */
$users = getUsers();

// Narrows down the type to `array`
$users = type($users)->not()->null();
```

And one more example, where we narrow down the type of a variable to a Collection without losing the type information:

```
/** @var Collection|null $users */
$users = getUsers();

// Narrows down the type to `Collection`
$users = type($users)->as(Collection::class);
```

Installation
------------

[](#installation)

> **Requires [PHP 8.2+](https://php.net/releases/)**

You may use [Composer](https://getcomposer.org) to install Type Guard into your PHP project:

```
composer require pinkary-project/type-guard
```

Usage
-----

[](#usage)

- [`as`](#as)
- [`asInt()`](#asint)
- [`asFloat()`](#asfloat)
- [`asString()`](#asstring)
- [`asBool()`](#asbool)
- [`asNull()`](#asnull)
- [`asCallable()`](#ascallable)
- [`not()->null()`](#notnull)
- [`asArray()`](#asarray)
- [`asIterable()`](#asiterable)

### `as`

[](#as)

Asserts and narrows down the type of the given variable to a more specific type.

```
$variable = type($variable)->as(User::class);
```

### `asInt()`

[](#asint)

Asserts and narrows down the type of the given variable to an integer.

```
$variable = type($variable)->asInt();
```

### `asFloat()`

[](#asfloat)

Asserts and narrows down the type of the given variable to a float.

```
$variable = type($variable)->asFloat();
```

### `asString()`

[](#asstring)

Asserts and narrows down the type of the given variable to a string.

```
$variable = type($variable)->asString();
```

### `asBool()`

[](#asbool)

Asserts and narrows down the type of the given variable to a boolean.

```
$variable = type($variable)->asBool();
```

### `asNull()`

[](#asnull)

Asserts and narrows down the type of the given variable to a null.

```
$variable = type($variable)->asNull();
```

### `asCallable()`

[](#ascallable)

Asserts and narrows down the type of the given variable to a callable.

```
$variable = type($variable)->asCallable();
```

### `not()->null()`

[](#not-null)

Asserts and narrows down the type of the given variable to a non-null value.

```
$variable = type($variable)->not()->null();
```

### `asArray()`

[](#asarray)

Asserts and narrows down the type of the given variable to an array.

```
$variable = type($variable)->asArray();
```

### `asIterable()`

[](#asiterable)

Asserts and narrows down the type of the given variable to an iterable.

```
$variable = type($variable)->asIterable();
```

---

**Type Guard** is part of the [Pinkary Project](https://github.com/pinkary-project) project. It was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** and open-sourced under the **[MIT license](https://opensource.org/licenses/MIT)**.

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance59

Moderate activity, may be stable

Popularity52

Moderate usage in the ecosystem

Community35

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

841d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/86cfef5c1f5195df1a9db17a5f8ecb34455e1f0133a725de9acf7f2fb26ac6a1?d=identicon)[nunomaduro](/maintainers/nunomaduro)

---

Top Contributors

[![nunomaduro](https://avatars.githubusercontent.com/u/5457236?v=4)](https://github.com/nunomaduro "nunomaduro (56 commits)")[![faissaloux](https://avatars.githubusercontent.com/u/60013703?v=4)](https://github.com/faissaloux "faissaloux (18 commits)")[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (3 commits)")[![ilyosjon09](https://avatars.githubusercontent.com/u/8179000?v=4)](https://github.com/ilyosjon09 "ilyosjon09 (3 commits)")[![hermy1](https://avatars.githubusercontent.com/u/49494077?v=4)](https://github.com/hermy1 "hermy1 (2 commits)")[![lmottasin](https://avatars.githubusercontent.com/u/68915904?v=4)](https://github.com/lmottasin "lmottasin (2 commits)")[![hungthai1401](https://avatars.githubusercontent.com/u/22017922?v=4)](https://github.com/hungthai1401 "hungthai1401 (2 commits)")[![rodelias-frete](https://avatars.githubusercontent.com/u/281798633?v=4)](https://github.com/rodelias-frete "rodelias-frete (1 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")[![james-astalty](https://avatars.githubusercontent.com/u/134993638?v=4)](https://github.com/james-astalty "james-astalty (1 commits)")[![CamKem](https://avatars.githubusercontent.com/u/112100521?v=4)](https://github.com/CamKem "CamKem (1 commits)")

---

Tags

phpstdlibtype-guardphptypeassertnarrowtype-guardpinkary-project

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pinkary-project-type-guard/health.svg)

```
[![Health](https://phpackages.com/badges/pinkary-project-type-guard/health.svg)](https://phpackages.com/packages/pinkary-project-type-guard)
```

###  Alternatives

[phpoption/phpoption

Option Type for PHP

2.7k592.5M192](/packages/phpoption-phpoption)[zakirullin/mess

Convenient array-related routine &amp; better type casting

21331.4k2](/packages/zakirullin-mess)[strictus/strictus

Strict Typing for local variables in PHP

1617.6k](/packages/strictus-strictus)[garoevans/php-enum

Convenient way to always have an Enum object available and utilise Spl Types if available.

19167.3k5](/packages/garoevans-php-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
