I have this type (shortened here for example):
type Form = {
firstName: string,
lastName: string,
age: number
};
I want to create two functions, one which accepts any key of Form where the value is of type string, and one for number. So the first one should accept "firstName" | "lastName" and the second one "age"
I also want to be able to index the object with the key and get back the correct type. E.g. for the string function:
function stringKey<... magic ...>(form: Form, key: ...) {
form[key] // should be of type string
}
I made a guess and tried this but it's invalid syntax. Is it possible to do this in TypeScript?
function stringKey<K extends keyof Form, Form[K] extends string>()