Well, I've reported this problem as an issue in TypeScript's official repository, and here's the reply from RyanCavanaugh, which solves this problem.
type a = { foo: "bar" } extends { "foo" } ? true : false;
type b = { [K in keyof { "foo" }]: { foo: "bar" } extends { K } ? true : false }["foo"];
The type being resolved here (annotated for additional clarity) is
{ foo: "bar" } extends { K: any } ? true : false
K in this position is a property name, not a reference to the key type. You could equally have written
type a = { foo: "bar" } extends { "foo" } ? true : false;
type b = { [K in keyof { "foo" }]: { foo: "bar" } extends { QQQ } ? true : false }["foo"];
Which is clearly correctly false
This is true as expected:
type b = { [K in keyof { "foo": any }]: ({ foo: "bar" } extends Record<K, unknown> ? true : false) }["foo"];