I am curious as to why the ReturnType
can accurately infer a function's specific return values when an if
statement is present, but only determines the type of the value when the function returns a guaranteed string or number.
For example, in the function testA
, ReturnType<typeof testA>
successfully infers the return values as 1 and 2.
On the other hand, for the function testB
which only returns one value, ReturnType<typeof testB>
can only infer that the type is number
.
Why is it that a function returning just one value can accurately deduce the type of the return value, such as number
, rather than the actual value?
This behavior also applies to string
values (as shown in the second screenshot).