I have a general understanding that type assertions are bad. Given that, I'm trying to write the following function without making use of one:
function stringifyQuery<T extends Record<string, unknown>>(
query: T
): { [K in keyof T]: string } {
return Object.fromEntries(
Object.entries(query).map(([key, value]) => {
return [key, JSON.stringify(value)]
})
)
}
but I get the type error: TS2322: Type '{ [k: string]: string; }' is not assignable to type '{ [K in keyof T]: string; }'.
Is there a way to convince the compiler this is correct without resorting to a type assertion?
I'm also open to changing the implementation if there's some more type-safe functions that can accomplish this.