Suppose I have a function called func
with 2 generic arguments
const func = <T extends {}, K extends keyof T>() => {};
and a type defined as
interface Form {
a: boolean;
b: string;
}
then I can call them without encountering any errors
func<Form, "a">();
func<Form, "b">();
Now, let's say I want the function to only accept keys for which T[K] = string
In simpler terms
func<Form, "a">(); // should fail
func<Form, "b">(); // should pass
I attempted a pseudo-Typescript solution like this
const func = <T extends {}, K extends keyof T : where T[K] extends string>() => {};
however, it didn't go very far. Is such restriction even possible? I would appreciate any guidance on this matter.