8.1.1
Type Testing
With the exactly
function you can test if types or values are an exact match to a type
const a: 1 | 2 = 1
//values (also does a runtime assertion on the values)
exactly(1 as number, a) // error as `1 | 2` is not an exact match of `number`
exactly(1 as number, a as number) // no error
exactly(1 as 1 | 2, a) // no error
// mixed
exactly<number>()(a) // error as `1 | 2` is not an exact match of `number`
exactly<number>()(a as number) // no error
exactly<1 | 2>()(a) // no error
// types
type Foo = 1 | 2
exactly<1, Foo>() // error as `1 | 2` is not an exact match of `1`
exactly<1 | 2, Foo>() // no error
The Equals
type allows you to check if two types are equal at the type level
Equals<number, 1 | 2> // false
Equals<any, 10> // false
Equals<unknown, never> // false
thanks to @KotlinIsland for helping out with this