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.
Union types
Section titled “Union types”Each union type is separated by a pipe character (|) and may contain any other
type definition.
A | B | C
resultTypeLang\Type\LogicalTypeNode\UnionTypeNode
Intersection types
Section titled “Intersection types”Each intersection type is separated by an ampersand character (&) and may
contain any other type definition.
A & B & C
resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode
Nullable types
Section titled “Nullable types”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
resultTypeLang\Type\WrappingTypeNode\NullableTypeNode
The question mark must be placed before the type name.
Example?
errorUnexpectedTokenException Unexpected "?"
Precedence
Section titled “Precedence”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
resultTypeLang\Type\LogicalTypeNode\UnionTypeNode
Read as (?A) | B.
?A | B
resultTypeLang\Type\LogicalTypeNode\UnionTypeNode
Read as (?A) & B.
?A & B
resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode
Read as ?(A[]).
?A[]
resultTypeLang\Type\WrappingTypeNode\NullableTypeNode
Parentheses
Section titled “Parentheses”Parentheses can be used to disambiguate types.
Disjunctive form (DNF).
(A & B) | C
resultTypeLang\Type\LogicalTypeNode\UnionTypeNode
Conjunctive form (CNF).
(A | B) & C
resultTypeLang\Type\LogicalTypeNode\IntersectionTypeNode