The first type for comparison. Can be any TypeScript type including:
The second type for comparison. Same constraints as type A
.
A string literal representing the expected type
relationship. TypeScript's type system automatically infers and restricts
the available operators based on the actual relationship between types A
and B
. If an invalid relationship is specified, TypeScript will show a
compilation error.
Combine expectType
with runtime assertions for comprehensive testing:
A
and B
..test.mts
files alongside runtime
assertions.
Compile-time type assertion utility for TypeScript type testing.
This function performs static type relationship checking at compile-time and has no runtime effect. It is primarily used in test files to verify that TypeScript's type inference and type relationships work as expected. The function will cause TypeScript compilation errors if the specified type relationship does not hold.
Supported Type Relations
Equality Relations
"="
(strict equality): Asserts that typesA
andB
are exactly the same type. Uses TypeScript's internal type equality checking."!="
(strict inequality): Asserts that typesA
andB
are not exactly the same type.Assignability Relations
"~="
(mutual assignability): Asserts thatA
extendsB
ANDB
extendsA
. Types are structurally equivalent and mutually assignable."<="
(subtype relation): Asserts that typeA
extends (is assignable to) typeB
. TypeA
is a subtype ofB
.">="
(supertype relation): Asserts that typeB
extends (is assignable to) typeA
. TypeA
is a supertype ofB
.Negative Assignability Relations
"!<="
(not subtype): Asserts that typeA
does NOT extend typeB
."!>="
(not supertype): Asserts that typeB
does NOT extend typeA
.Type Parameter Constraints