I'm trying to make a primary row Id constraint right now for some generic forms I'm making. I need a constraint that allows people to specify the name of the property on T, such that the value is of type z. I'll explain in more detail.
Constraint should basically mean, "give me the name of any property on type T where the value of T[propertyName] is of type V."
The important thing here is to make it work with generics. Also... to allow accessing properties via the property key afterwards.
interface Model {
text?: string;
id: number;
uniqueId: string;
}
interface SomeProps<T> {
model: T;
primaryKey: KeyOfType<Model, number>;
}
function testFunction(props: SomeProps<Model>) {
console.log('The primary key is: '+ props.model[props.primaryKey]); // 99
}
testFunction({ model: { id: 99, text: 'hello universe', uniqueId: 'ssfwe3234'}, primaryKey: 'id'});