Is there a way to create an arrow function using rest parameters that can return different types based on the number of arguments passed?
For example, I am looking to implement a safeId()
function with the following return type variations:
safeId() // () => string
safeId('foo') // (obj: any) => string
safeId('foo', 4, {}, ['hi']) // (...obj: any[]) => string[]
// Currently, the return type is not specific:
const safeId = (...objects: any[]) => string | string[] => {
// Simplified functionality
if (objects.length > 1) return sanitizedObjectIds(objects) // string[]
return 'id'
}