Here is a code snippet to consider:
type TestTuple = [
{ test: "foo" },
{
test: "bar";
other: 1;
}
];
type Foo<Prop extends string> = TestTuple extends Record<Prop, string>[]
? true
: false;
type X = Foo<"test">;
type Prop = "test";
type Y = TestTuple extends Record<Prop, string>[]
? true
: false;
// X is type false
const x: X = false;
// Y is type true
const y: Y = true;
Types Foo
and Y
are quite similar. However, the main difference is that Foo
has a generic parameter Prop
, while Y
simply uses a type alias named Prop
. Even though it's not necessary, I wanted their declarations to mirror each other exactly. Therefore, shouldn't Foo<"test">
(represented by the type X
) and Y
have the same outcomes? Surprisingly, they do not. X
results in type false
, whereas Y
evaluates to type true
. Interestingly, altering the other
property in
TestTuple</code to a string or removing it completely causes both <code>X
and Y
to evaluate as true, which aligns with expectations.
My question then becomes: why is this discrepancy happening? Could it be a bug in the compiler? If so, has an issue already been reported but remains elusive to me? Or, perhaps this behavior is related to how generics are dealt with in TypeScript?