I have:
interface MyInterface {
a: string;
b: number;
}
I have another general-purpose function:
myFunction(myInstance: MyInterace, fieldName: string) {
let field = myInstance[fieldName];
// do something with this field
}
Usage being:
myFunction(myInst, "a");
or
myFunction(myInst, "b");
However, I would like to pass "a" or "b" in a type-safe manner. That means that if I were to change MyInterace a field to a2, the call used in myFunction(myInst, "a") wouldn't compile and I would be able to find in compilation time all these places that got broken due to renaming.
I tried using Pick but couldn't get the name of the field from it.
Any suggestions? I would imagine that there should be some:
MyInterace::a from which I could extra the string "a".