Is there a way to enforce a function's argument to be required if it has keys in the inferred type, and optional if it does not exist or is an empty object?
I've experimented with conditionals and overloads but have yet to find a solution.
const MyFunc = <T extends {}>(vars?: T): void => {
// ...
}
// Should be allowed
MyFunc()
// Should be allowed
MyFunc<{}>()
// Should not be allowed - the argument should be required
MyFunc<{ a: string }>()
I've tried using overloads and conditional types like keyof T extends never ? ...
, but I am stuck. Is this achievable with current TypeScript features?
Thank you!