I'm having trouble with TypeScript generics
My issue is as follows:
I have an interface called 'Column' and a function called makeColumn to create a new column. I want to only set the first generic (UserModel), where the accessor must be a key of UserModel, and then get the value of the name property in the render property.
This is what I've tried:
class UserModel{
name!:{
firstName:string;
secondName:string;
}
job!:string
}
interface Column<T ,U extends keyof T> {
accessor: U;
render: (value: T[U]) => void;
}
function makeColumn<T,U extends keyof T >(column: Column<T,U>) {
return column;
}
makeColumn<UserModel>({
accessor: 'name',
render: (value) => {
console.log(value.)
}
})
Note: I do not want to pass a second generic parameter to access the name property, I want it to handle that on its own.