Upon examination,
type B = {
foo: string;
bar: number;
};
function get<F extends B, K extends keyof B>(f: F, k: K): F[K] {
return f[k];
}
It seems like a similar concept is expressed in a different way in the following code snippet:
function get<F extends B, K extends keyof B>(f: F, k: K): F extends B ? F[K] : undefined {
return f[k];
}
However, an error occurs during type checking:
Type 'F[K]' is not assignable to type 'F extends B ? F[K] : undefined'.
Type 'F["foo"] | F["bar"]' is not assignable to type 'F extends B ? F[K] : undefined'.
Type 'F["foo"]' is not assignable to type 'F extends B ? F[K] : undefined'.
Type 'string' is not assignable to type 'F extends B ? F[K] : undefined'.
This raises a question about what additional information the TS compiler has about types that may not be immediately obvious.