I have a TypeScript function a bit like this:
function lookup<Object, Key extends keyof Object>(object: Object, key: Key): any
Now, can I constrain the key type using the type of the produced value? Example:
interface Animal {
name: string;
legs: number;
}
Now can I constrain lookup
to only allow keys that have a string
value? Ie. that calling lookup(animal, "name")
would be valid, but lookup(animal, "legs")
would be not.