PHPackages                             hhvm/type-assert - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. hhvm/type-assert

AbandonedArchivedLibrary[Validation &amp; Sanitization](/categories/validation)

hhvm/type-assert
================

Convert untyped data to typed data

v4.2.2(4y ago)251.1M↓27.9%13[3 issues](https://github.com/hhvm/type-assert/issues)[2 PRs](https://github.com/hhvm/type-assert/pulls)20MITHack

Since Sep 28Pushed 2y ago16 watchersCompare

[ Source](https://github.com/hhvm/type-assert)[ Packagist](https://packagist.org/packages/hhvm/type-assert)[ RSS](/packages/hhvm-type-assert/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (45)Used By (20)

TypeAssert
==========

[](#typeassert)

[![Continuous Integration](https://github.com/hhvm/type-assert/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/hhvm/type-assert/actions/workflows/build-and-test.yml)

Hack library for converting untyped data to typed data.

Warning for `TypeAssert\matches_type_structure()`
-------------------------------------------------

[](#warning-for-typeassertmatches_type_structure)

`TypeStructure`, `type_structure()`, and `ReflectionTypeAlias::getTypeStructures()`are experimental features of HHVM, and not supported by Facebook or the HHVM team. This means that `matches_type_structure()` may need to be removed in a future release without warning.

We strongly recommend moving to `TypeAssert\matches()` and `TypeCoerce\match()` instead.

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

[](#installation)

```
composer require hhvm/type-assert

```

Usage
-----

[](#usage)

TypeAssert provides functions that take a mixed input, and will either return it unmodified (but with type data) or throw an exception; for example:

```
 123]); // passes: $x is a dict
$x = $spec->assertType(dict['foo' => '123']); // fails
$x = $spec->assertType(dict[123 => 456]); // fails
$x = $spec->assertType(dict[123 => 456]); // fails

$x = $spec->coerceType(dict[123 => '456']); // passes: $x is dict['123' => 456];
```

Shapes and tuples are not supported, as they can not be expressed generically.

`matches_type_structure(TypeStructure, mixed): T`
-------------------------------------------------------

[](#matches_type_structurettypestructuret-mixed-t)

Asserts that a variable matches the given type structure; these can be arbitrary nested shapes. This is particular useful for dealing with JSON responses.

```
 int,
    'user' => string,
    'data' => shape(
      /* ... */
    ),
  );

  public static function getAPIResponse(): self::TAPIResponse {
    $json_string = file_get_contents('https://api.example.com');
    $array = json_decode($json_string, /* associative = */ true);
    return TypeAssert\matches_type_structure(
      type_structure(self::class, 'TAPIResponse'),
      $array,
    );
  }
}
```

You can use `type_structure()` to get a `TypeStructure` for a type constant, or `ReflectionTypeAlias::getTypeStructure()` for top-level type aliases.

`not_null(?T): T`
--------------------

[](#not_nulltt-t)

Throws if it's null, and refines the type otherwise - for example:

```
 string
  needs_int(TypeAssert\not_null($bar)); // ?int => int
}
```

`is_instance_of(classname, mixed): T`
-------------------------------------------

[](#is_instance_oftclassnamet-mixed-t)

Asserts that the input is an object of the given type; for example:

```
