Skip to content

Syntax

Conditional types

A conditional (ternary) type describes a type that depends on the result of comparing a subject with some other type. It consists of a condition, followed by a question mark (?), the type used if the condition is true, a colon (:), and the type used if the condition is false.

<Subject> <Operator> <Type> ? <TypeIfTrue> : <TypeIfFalse>
T is int ? "int type" : "not int type"
conditional
is
type
type
literal
literal

resultTypeLang\Type\Condition\ConditionalTypeNode

Both the subject and the type being compared against can be a union (and so any type below it: an intersection, a nullable, a list, etc.) or a variable ($name). The branches can be any type, including another conditional type, so the chain T is A ? B : C is D ? E : F is read as T is A ? B : (C is D ? E : F).

A named type as the subject.

T is int ? A : B
conditional
is
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

A variable as the subject.

$value is int ? A : B
conditional
is
variable
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

A union type as the subject.

T is A|B ? C : D
conditional
is
type
union
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

A callable type as the subject.

foo() is int ? A : B
conditional
is
callable
params
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

Parentheses may be used to disambiguate or nest conditional types.

T is int ? (U is string ? A : B) : C
conditional
is
type
type
conditional
is
type
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

The is keyword checks whether the subject is of the compared type, and is not checks the opposite. Both keywords are written in lower case only: IS is an ordinary name, not a keyword. Whitespace and comments are allowed between the words of is not.

Positive equality check.

T is int ? A : B
conditional
is
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

Negative equality check.

T is not int ? A : B
conditional
is not
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

A comment between the words of is not.

T is /* comment */ not int ? A : B
conditional
is not
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

Since the subject and the compared type can both be arbitrary type expressions, the condition may also be written in Yoda-style, with the concrete type on the left and the subject (a type or a variable) on the right.

Yoda-style equality check with a named type as the subject.

int is T ? A : B
conditional
is
type
type
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

Yoda-style equality check with a variable as the subject.

int is $value ? A : B
conditional
is
type
variable
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode

A variable may also stand on both sides.

$value is not $other ? A : B
conditional
is not
variable
variable
type
type

resultTypeLang\Type\Condition\ConditionalTypeNode