In typescript, we can use index types like this:
interface Dummy {
name: string;
birth: Date;
}
function doSomethingOnProperty<T, K extends keyof T>(o: T, name: K): void {
o[name];
}
var dummy = { name: "d", birth: new Date() };
doSomethingOnProperty(dummy, "name");
Question :
How to add a general restriction to accept only the property name of a certain type (is this possible?):
function doSomethingOnDATEProperty<T, K extends keyof T>(o: T, name: K): void {
o[name];
}
doSomethingOnDATEProperty(dummy, "name");
source
share