I have a function that takes an object as an input and returns an object as well. I would like to define it such that it can have any number of keys in the input or returned object.
Right now, I define it like this:
const myFunc: ({}) => ({})
Is there a good way to write it to avoid any type errors while also using the advantages that TS provides?
You can use TypeScript's Record<K, V> type to specify an object with arbitrary keys of type K and values of type V:
type MyObj = Record<string, any>;
Keys must be strings, numbers, or symbols, but values can be any type you specify. Your function would then become:
const myFunc = (obj: Record<???, ???>): Record<???, ???> => { /* ... */ };