Skip to content

Syntax

Logical types

The TypeLang, like PHP, supports two types of composite (logical) types: Union and Intersection. The ability to specify a nullable type using a separate expression is also supported.

Each union type is separated by a pipe character (|) and may contain any other type definition.

A | B | C
union
type
type
type

resultTypeLang\Type\LogicalTypeNode\UnionTypeNode

Each intersection type is separated by an ampersand character (&) and may contain any other type definition.

A & B & C
inter
type
type
type

resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode

Nullable type is a shortened alias for union type T | null and is written as ?T.

Description of nullable type Example (equivalent to Example | null)

?Example
nullable
type

resultTypeLang\Type\WrappingTypeNode\NullableTypeNode

An intersection binds tighter than a union, and a nullable marker binds tighter than both of them, so the parentheses are needed only to change the order.

Read as A | (B & C).

A | B & C
union
type
inter
type
type

resultTypeLang\Type\LogicalTypeNode\UnionTypeNode

Read as (?A) | B.

?A | B
union
nullable
type
type

resultTypeLang\Type\LogicalTypeNode\UnionTypeNode

Read as (?A) & B.

?A & B
inter
nullable
type
type

resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode

Read as ?(A[]).

?A[]
nullable
list
type

resultTypeLang\Type\WrappingTypeNode\NullableTypeNode

Parentheses can be used to disambiguate types.

Disjunctive form (DNF).

(A & B) | C
union
inter
type
type
type

resultTypeLang\Type\LogicalTypeNode\UnionTypeNode

Conjunctive form (CNF).

(A | B) & C
inter
union
type
type
type

resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode