Search by

type-lang / types

Serafim

An AST nodes of the TypeLang

Package info

github.com/php-type-language/types

pkg:composer/type-lang/types

Statistics

Installs: 1 852

Dependents: 4

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.0-beta3 2026-11-10 20:47 UTC

This package is auto-updated.

Last update: 2026-11-10 20:57:23 UTC


README

PHP 8.1+ Latest Stable Version Latest Unstable Version License MIT

The AST node classes (TypeLang\Type\*) for TypeLang — a declarative type language inspired by static analyzers like PHPStan and Psalm.

These plain, dependency-free DTOs are the shared vocabulary of the TypeLang ecosystem: the parser produces them, the printer renders them, and the reader builds them from Reflection.

Full documentation is available at typelang.dev.

Installation

Install the package via Composer:

composer require type-lang/types

Requirements:

  • PHP 8.1+

Usage

A type is a tree of typed nodes instead of a string, so tools can inspect and change it without parsing text. You usually get the tree from the parser, but it can be built by hand as well. Every node also keeps the $offset of its token in the source.

// Example<string, int, *>|null
$type = new UnionTypeNode([
    new NamedTypeNode(
        name: new Name('Example'),
        arguments: new TemplateListNode([
            new TypedTemplateArgumentNode(
                new NamedTypeNode(new Name('string')),
            ),
            new TypedTemplateArgumentNode(
                new NamedTypeNode(new Name('int')),
            ),
            new WildcardTemplateArgumentNode()
        ]),
    ),
    new NullLiteralNode(),
]);

// Read the tree
$type[0]->name->value;                    // 'array'
$type[0]->arguments[1]->value->name->value; // 'int'

// Change it: the tree is mutable, names and literals are immutable values
$type[0]->name = new Name('iterable');

The package covers the full type grammar: unions and intersections, generics, shapes, callables, conditional types, class constants and their masks, and literals. See the documentation for the node reference.