Consider the following enum A:
enum A {
ONE,
TWO = "TWO"
}
I want to write a generic type which checks if a string or number is part of A. This is what I have come up with:
type IsAValue<T extends string | number> = T extends string
? T extends `${A}`
? "true1"
: "false1"
: `${T}` extends `${A}`
? "true2"
: "false2"
This seems to work great.
type Test1 = IsAValue<0>
// Test1 = "true2"
type Test2 = IsAValue<A.ONE>
// Test2 = "true2"
type Test3 = IsAValue<"TWO">
// Test3 = "true1"
type Test4 = IsAValue<A.TWO>
// Test4 = "true1"
type Test5 = IsAValue<9999>
// Test5 = "false2"
type Test6 = IsAValue<"NOT IN ENUM">
// Test6 = "false1"
But now I don't want to return "true1" or "true2" but instead just return T.
type IsAValue2<T extends string | number> = T extends string
? T extends `${A}`
? T
: "false1"
: `${T}` extends `${A}`
? T
: "false2"
Yet unexpectedly this breaks for A.TWO.
type Test7 = IsAValue2<A.TWO>
// Test7 = never
This is strange since we just saw that it evaluates to "true1" in IsAValue. When I return T I would simply expect A.TWO to be the output here.
Why does this resolve to never if never is not even a type in any branch of IsAValue2. And why does this still work for the other types?