I am using nop function that gives correct typing for a Record-like structure for auto completion purposes that looks like this:
export const Autocompletion_Record = <T extends { [key: string]: any }>(arg: T): T => {
return arg;
}
const record = Autocompletion_Record({first: "string", second: 42})
This gives correct typing on any property that is passed within object data structure. I would really like to be able to specify types of objects that can be those properties.
I could create new helper function for every case which is not really nice like this:
export const Restricted_Autocompletion_Record = <T extends { [key: string]: string }>(arg: T): T => {
return arg;
}
//error here
const restricted_record = Restricted_Autocompletion_Record({first: "string", second: 42})
What I need is to parametrize this, but the issue is that I need to provide all type parameters here, but that is really out of question as auto inference of that structure is most crucial part of that function to begin with.
Is there some TS Type trick that I am not aware of that I could use?
Or perhaps some trick related to default generic type values?
Intended usage would be like this:
export const Intended_Autocompletion_Record = <Type, T extends { [key: string]: Type }>(arg: T): T => {
return arg;
}
/there would now be error if number prop was passed
const intended_usage = Intended_Autocompletion_Record<string>({first: "string", second: "another"})