PHPackages                             type-lang/types - 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. type-lang/types

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

type-lang/types
===============

An AST nodes of the TypeLang

0253↑1251.8%

Since Jul 5Compare

[ Source](https://github.com/php-type-language/types)[ Packagist](https://packagist.org/packages/type-lang/types)[ RSS](/packages/type-lang-types/feed)WikiDiscussions Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

[ ![](https://github.com/php-type-language/.github/raw/master/assets/dark.png?raw=true)](https://github.com/php-type-language) [![PHP 8.1+](https://camo.githubusercontent.com/8b914b5bf0cea5533e30755fe3693c4feb92f38cd2fafab8e43526c90e83d5a2/68747470733a2f2f706f7365722e707567782e6f72672f747970652d6c616e672f74797065732f726571756972652f7068703f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/type-lang/types) [![Latest Stable Version](https://camo.githubusercontent.com/9e890298c516a0bc63db0d4b5fd892e23df41d06cff729335adfb229cd085a19/68747470733a2f2f706f7365722e707567782e6f72672f747970652d6c616e672f74797065732f76657273696f6e3f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/type-lang/types) [![Latest Unstable Version](https://camo.githubusercontent.com/4b59aa381f71c5de4ff402981df9daac72e50029aa99877e3006c491c36d9511/68747470733a2f2f706f7365722e707567782e6f72672f747970652d6c616e672f74797065732f762f756e737461626c653f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/type-lang/types) [![License MIT](https://camo.githubusercontent.com/4e24f27775258ced851675723a4de08dcfc8716ca1f6c4d96f2e8d0bc42c8391/68747470733a2f2f706f7365722e707567782e6f72672f747970652d6c616e672f74797065732f6c6963656e73653f7374796c653d666f722d7468652d6261646765)](https://raw.githubusercontent.com/php-type-language/types/blob/master/LICENSE)

 [![](https://github.com/php-type-language/types/workflows/tests/badge.svg)](https://github.com/php-type-language/types/actions)

---

TypeLang DTO Types
==================

[](#typelang-dto-types)

AST node classes for the TypeLang type system.

**TypeLang** is a declarative type language inspired by static analyzers like [PHPStan](https://phpstan.org/) and [Psalm](https://psalm.dev/docs/).

Read [documentation pages](https://typelang.dev) for more information.

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

[](#installation)

The package is available as a Composer repository and can be installed using the following command in a root of your project:

```
composer require type-lang/types
```

Requirements
------------

[](#requirements)

- PHP 8.4+

Node Overview
-------------

[](#node-overview)

All nodes extend the abstract `Node` class and expose a single common property:

```
// Byte offset of the token in the original source string.
public int $offset = 0;
```

### `Identifier`

[](#identifier)

A single name segment such as `string`, `MyClass`, or `non-empty-string`. Virtual identifiers (containing `-`) are common in PHPStan/Psalm type aliases.

```
$id = Identifier::createFromString('  example  '); // trims whitespace

$id->value;      // 'example'
$id->isVirtual;  // false
$id->isBuiltin;  // false
$id->isSpecial;  // false

//
// Virtual vs. Builtin vs. Special
//

// e.g. "array-key", "positive-int", etc.
$virtual = new Identifier('non-empty-string');
$virtual->isVirtual;  // true (contains "-")
$virtual->isBuiltin;  // false
$virtual->isSpecial;  // false

// e.g. "float", "bool", "null", "true", etc.
$builtin = new Identifier('int');
$builtin->isVirtual;  // false
$builtin->isBuiltin;  // true
$builtin->isSpecial;  // false

// e.g. "self", "parent", etc.
$special = new Identifier('static');
$special->isVirtual;  // false
$special->isBuiltin;  // false
$special->isSpecial;  // true
```

### `Name`

[](#name)

A fully- or partially-qualified name composed of `Identifier` segments.

```
$name = Name::createFromString('\TypeLang\Parser\Node');

$name->isFullyQualified; // true
$name->isSimple;         // false
$name->first->value;     // 'TypeLang'
$name->last->value;      // 'Node'
$name->toString();       // '\TypeLang\Parser\Node'

$name->slice(1)
    ->toString();        // 'Parser\Node'

$name->toUnqualified()
    ->toString();        // 'TypeLang\Parser\Node'

$name->mergeWith(Name::createFromString('Node\Sub'))
    ->toString();        // '\TypeLang\Parser\Node\Sub'
```

### Type Nodes

[](#type-nodes)

All type nodes extend the abstract `TypeNode` class.

#### `NamedTypeNode`

[](#namedtypenode)

The most common node — a named type with optional template arguments and shape fields.

```
// int
new NamedTypeNode(Name::createFromString('int'));

// array
new NamedTypeNode(
    name: Name::createFromString('array'),
    arguments: new TemplateArgumentListNode([
        new TemplateArgumentNode(new NamedTypeNode(
            Name::createFromString('string'),
        )),
        new TemplateArgumentNode(new NamedTypeNode(
            Name::createFromString('int'),
        )),
    ]),
);
```

#### `NullableTypeNode`

[](#nullabletypenode)

Wraps a type to make it nullable (`?Type`).

```
new NullableTypeNode(new NamedTypeNode(Name::createFromString('string')));
```

#### `UnionTypeNode` / `IntersectionTypeNode`

[](#uniontypenode--intersectiontypenode)

Represent `A|B|C` and `A&B&C` respectively. Nested unions (or intersections) of the same kind are automatically flattened.

```
$union = new UnionTypeNode(
    new NamedTypeNode(Name::createFromString('int')),
    new NamedTypeNode(Name::createFromString('string')),
    new NamedTypeNode(Name::createFromString('null')),
);

$intersection = new IntersectionTypeNode(
    new NamedTypeNode(Name::createFromString('int')),
    new NamedTypeNode(Name::createFromString('string')),
    new NamedTypeNode(Name::createFromString('null')),
);
```

#### `TypesListNode`

[](#typeslistnode)

Represents the array-shorthand `Type[]`.

```
// int[]
new TypesListNode(new NamedTypeNode(Name::createFromString('int')));
```

#### `TypeOffsetAccessNode`

[](#typeoffsetaccessnode)

Represents an indexed access type `T[K]`.

```
new TypeOffsetAccessNode(
    type:   new NamedTypeNode(Name::createFromString('T')),
    access: new NamedTypeNode(Name::createFromString('K')),
);
```

#### `CallableTypeNode`

[](#callabletypenode)

Represents a callable signature.

```
// callable(int, string): bool
new CallableTypeNode(
    name: Name::createFromString('callable'),
    parameters: new CallableParameterListNode([
        new CallableParameterNode(
            type: new NamedTypeNode(Name::createFromString('int')),
        ),
        new CallableParameterNode(
            type: new NamedTypeNode(Name::createFromString('string')),
        ),
    ]),
    type: new NamedTypeNode(Name::createFromString('bool')),
);
```

#### `TernaryExpressionNode`

[](#ternaryexpressionnode)

Represents a conditional type expression `subject is Target ? Then : Else`.

```
new TernaryExpressionNode(
    condition: new EqualConditionNode(
        subject: new NamedTypeNode(Name::createFromString('T')),
        target:  new NamedTypeNode(Name::createFromString('string')),
    ),
    then: new NamedTypeNode(Name::createFromString('non-empty-string')),
    else: new NamedTypeNode(Name::createFromString('T')),
);
```

#### `ConstMaskNode` / `ClassConstNode` / `ClassConstMaskNode`

[](#constmasknode--classconstnode--classconstmasknode)

Represent constant references and wildcard masks.

```
// Status::ACTIVE
new ClassConstNode(
    class:    Name::createFromString('Status'),
    constant: new Identifier('ACTIVE'),
);

// Status::*
new ClassConstMaskNode(class: Name::createFromString('Status'));

// Foo\Bar\*
new ConstMaskNode(name: Name::createFromString('Foo\Bar'));
```

### Literal Nodes

[](#literal-nodes)

All literals extend `LiteralNode` and expose `$value` (native PHP type) and `$raw` (original source token). Most support a static `parse()` factory.

```
// value: true,  raw: 'true'
BoolLiteralNode::parse('true');
// value: false, raw: 'False'
BoolLiteralNode::parse('False');

// value: 255,   raw: '0xFF', decimal: '255'
IntLiteralNode::parse('0xFF');
// value: 10,    raw: '0b1010'
IntLiteralNode::parse('0b1010');
// value: 1000,  raw: '1_000'
IntLiteralNode::parse('1_000');

// value: 150.0, raw: '1.5e2'
FloatLiteralNode::parse('1.5e2');

// value: null,  raw: 'Null'
new NullLiteralNode('Null');

// decodes escape sequences
StringLiteralNode::parse('"hello\nworld"');
// no escape decoding
StringLiteralNode::parse("'raw'");

// value: 'name' (no $), raw: '$name'
VariableLiteralNode::parse('$name');
```

### Condition Nodes

[](#condition-nodes)

Used as the `$condition` of `TernaryExpressionNode`. All extend `Condition`and hold `public TypeNode $subject` and `public TypeNode $target`.

ClassMeaning`EqualConditionNode``subject is target``NotEqualConditionNode``subject is not target``GreaterThanConditionNode``subject > target``GreaterThanOrEqualConditionNode``subject >= target``LessThanConditionNode``subject < target``LessThanOrEqualConditionNode``subject
