https://github.com/typhoon-php/type
Typhoon Type System
https://github.com/typhoon-php/type
php php-library
Last synced: 2 months ago
JSON representation
Typhoon Type System
- Host: GitHub
- URL: https://github.com/typhoon-php/type
- Owner: typhoon-php
- License: mit
- Created: 2023-02-26T16:31:39.000Z (about 3 years ago)
- Default Branch: 0.8.x
- Last Pushed: 2026-02-11T00:12:03.000Z (3 months ago)
- Last Synced: 2026-02-11T03:36:42.649Z (3 months ago)
- Topics: php, php-library
- Language: PHP
- Homepage:
- Size: 795 KB
- Stars: 64
- Watchers: 5
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Typhoon Type
[](https://packagist.org/packages/typhoon/type)
[](https://github.com/typhoon-php/type/releases)
[](phpstan.dist.neon)
[](https://codecov.io/gh/typhoon-php/type/tree/0.8.x)
[](https://dashboard.stryker-mutator.io/reports/github.com/typhoon-php/type/0.8.x)
Typhoon Type is an object abstraction over the modern PHP type system. Use this library to build tools that work with
sophisticated types:
```php
use function Typhoon\Type\arrayShapeT;
use function Typhoon\Type\objectT;
use function Typhoon\Type\nonEmptyListT;
$json = << nonEmptyListT(objectT(Person::class)),
]);
$request = new Mapper()->map($json, $type);
```
## Installation
```
composer require typhoon/type
```
## Printing types
To print any type, use `stringify()`:
```php
use function Typhoon\Type\stringify;
$type = arrayShapeT([
'people' => nonEmptyListT(objectT(Person::class)),
]);
var_dump(stringify($type));
// array{'people': non-empty-list}
```
## Supported types
### Native
| Typhoon | Native |
|-------------------------------------------------------------------------|-------------------------|
| `nullT` | `null` |
| `voidT` | `void` |
| `neverT` | `never` |
| `falseT` | `false` |
| `trueT` | `true` |
| `boolT` | `bool` |
| `intT` | `int` |
| `floatT` | `float` |
| `stringT` | `string` |
| `arrayT` | `array` |
| `objectT` | `object` |
| `objectT(Foo::class)` | `Foo` |
| `selfT` | `self` |
| `parentT` | `parent` |
| `staticT` | `static` |
| `iterableT` | `iterable` |
| `callableT` | `callable` |
| `resourceT` | `resource` |
| `nullOrT(stringT)` | `?string` |
| `unionT(intT, stringT)` | `int\|string` |
| `intersectionT(objectT(Countable::class), objectT(Traversable::class))` | `Countable&Traversable` |
| `mixedT` | `mixed` |
### PHPDoc numbers
| Typhoon | PHPDoc |
|----------------------------------------------------|---------------------------|
| `intT(123)` | `123` |
| `positiveIntT` | `positive-int` |
| `negativeIntT` | `negative-int` |
| `nonPositiveIntT` | `non-positive-int` |
| `nonNegativeIntT` | `non-negative-int` |
| `nonZeroIntT` | `non-zero-int` |
| `intRangeT(-5, 6)` | `int<-5, 6>` |
| `intRangeT(max: 6)` | `int` |
| `intRangeT(min: -5)` | `int<-5, max>` |
| `intMaskT(1, 2, 4)` | `int-mask<1, 2, 4>` |
| `intMaskT(classConstantMaskT(Foo::class, 'INT_*')` | `int-mask-of` |
| `floatT(12.5)` | `12.5` |
| `floatRangeT(-0.001, 2.344)` | `float<-0.001, 2.344>` |
### PHPDoc strings
| Typhoon | PHPDoc |
|---------------------------------------------------------------------------------|-------------------------------------|
| `nonEmptyStringT` | `non-empty-string` |
| `truthyStringT`, `nonFalsyStringT` | `truthy-string`, `non-falsy-string` |
| `numericStringT` | `numeric-string` |
| `lowercaseString` | `lowercase-string` |
| `stringT('abc')` | `'abc'` |
| `classT(Foo::class)`, `classStringT(Foo::class)`, `classT(objectT(Foo::class))` | `class-string` |
| `stringT(Foo::class)`, `classConstantT(Foo::class, 'class')` | `Foo::class` |
| `literalStringT` | `literal-string` |
### PHPDoc constants
| Typhoon | PHPDoc |
|------------------------------------------|---------------|
| `constantT('PHP_INT_MAX')` | `PHP_INT_MAX` |
| `constantMaskT('JSON_*')` | `JSON_*` |
| `classConstantT(Foo::class, 'BAR')` | `Foo::BAR` |
| `classConstantMaskT(Foo::class, 'IS_*')` | `Foo::IS_*` |
### PHPDoc arrays and iterables
| Typhoon | PHPDoc |
|-------------------------------------------------|--------------------------------------|
| `arrayT(value: objectT(Foo::class))` | `Foo[]` |
| `listT(stringT)` | `list` |
| `nonEmptyListT(stringT)` | `non-empty-list` |
| `listShapeT([intT, stringT])` | `list{int, string}` |
| `arrayShapeT([intT, optional(stringT)])` | `list{int, 1?: string}` |
| `unsealedListShapeT([intT])` | `list{int, ...}` |
| `unsealedListShapeT([intT], stringT)` | `list{int, ...}` |
| `arrayT(value: stringT)` | `array` |
| `arrayT(intT, stringT)` | `array` |
| `nonEmptyArrayT(arrayKeyT, stringT)` | `non-empty-array` |
| `arrayShapeT()` | `array{}` |
| `arrayShapeT([intT, stringT])` | `array{int, string}` |
| `arrayShapeT([intT, 'a' => optional(stringT)])` | `array{int, a?: string}` |
| `unsealedArrayShapeT([intT])` | `array{int, ...}` |
| `unsealedArrayShapeT([floatT], intT, stringT)` | `array{float, ...}` |
| `keyOfT(classConstantT(Foo::class, 'ARRAY'))` | `key-of` |
| `valueOfT(classConstantT(Foo::class, 'ARRAY'))` | `value-of` |
| `offsetT($TArray->type, $TKey->type)` | `TArray[TKey]` |
| `iterableT(objectT, stringT)` | `iterable` |
| `iterableT(value: stringT)` | `iterable` |
### PHPDoc objects
| Typhoon | PHPDoc |
|------------------------------------------------|-------------------------|
| `objectT(Foo::class, [stringT, floatT])` | `Foo` |
| `selfT([stringT, floatT])` | `self` |
| `parentT([stringT, floatT])` | `parent` |
| `staticT([stringT, floatT])` | `static` |
| `objectShapeT(['prop' => stringT])` | `object{prop: string}` |
| `objectShapeT(['prop' => optional(stringT))])` | `object{prop?: string}` |
### PHPDoc callables
| Typhoon | PHPDoc |
|-----------------------------------------------|------------------------------|
| `callableT([stringT], voidT)` | `callable(string): void` |
| `callableT([param(stringT, default: true)])` | `callable(string=): mixed` |
| `callableT([param(stringT, variadic: true)])` | `callable(string...): mixed` |
| `callableT([param(stringT, byRef: true)])` | `callable(string&): mixed` |
| `closureT([stringT], voidT)` | `Closure(string): void` |
| `closureT([param(stringT, default: true)])` | `Closure(string=): mixed` |
| `closureT([param(stringT, variadic: true)])` | `Closure(string...): mixed` |
| `closureT([param(stringT, byRef: true)])` | `Closure(string&): mixed` |
### Union aliases
| Typhoon | PHPDoc |
|-------------|-------------|
| `array-key` | `arrayKeyT` |
| `numericT` | `numeric` |
| `scalarT` | `scalar` |