What's the difference between the following two conditional types:
type IfVoid<P, True, False> = [void] extends [P] ? True : False
type IfVoid<P, True, False> = void extends P ? True : False
What's the difference between the following two conditional types:
type IfVoid<P, True, False> = [void] extends [P] ? True : False
type IfVoid<P, True, False> = void extends P ? True : False
Conditional types distribute over unions when there is a "naked type parameter" in the first part of an extends
clause, such that T extends A ? B : C
is equivalent to (T1 extends A ? B : C) | (T2 extends A ? B : C)
when T
is a union type like T1 | T2
. So the following two types are, in general, not equivalent because the first one distributes when T
is a union and the second one doesn't:
T extends A ? B : C
[T] extends [A] ? B : C
For a concrete example where they are different, see the example code below (Playground Link):
type Test1<T> = T extends 'foo' ? 'bar' : 'baz'
type Test2<T> = [T] extends ['foo'] ? 'bar' : 'baz'
type R1 = Test1<'foo' | 'bar'> // 'bar' | 'baz'
type R2 = Test2<'foo' | 'bar'> // 'baz'
For a practical example where it makes a difference, see this other Q&A.
However, in your case void
is not a type parameter, so it does not appear that there is any difference. Perhaps the code used to be different, or it was adapted from another piece of code where distributivity did make a difference.