Looking to create a function that acts as an interface, with generic parameters dictating key names and value types.
Trying to minimize repetition in calling code to keep it "DRY."
Struggling with using the generic type as an object key due to TypeScript errors, but determined to make it work!
Current approach:
function foo<K extends string, V>(name: K): Partial<{ [key in K]: V; }> {
return { [name]: undefined } as Partial<{ [key in K]: V; }>;
}
This setup requires passing the key name twice when making the call, which feels redundant.
Although TypeScript ensures consistency between the two occurrences of the key name, it still seems unnecessary from the caller's perspective.
After attempting alternative patterns, facing compiler errors but staying hopeful for a solution without complications.