If you want to combine all arguments together, you can utilize the U[number]
method:
function merge<U extends any[]>(...args: U): U[number] {
return args[Math.round(Math.random()*(args.length - 1))]; // example implementation
}
let result = merge(1,"2", true) // number | string | boolean
console.log(result)
You also have the option to retrieve the type at a specific position, but because the position may not always be present, a conditional type is necessary;
type GetAt<T extends any[], I extends number> = T extends Record<I, infer U> ? U : never;
function merge<U extends any[]>(...args: U): GetAt<U, 0> {
return args[Math.round(Math.random()*(args.length - 1))]; // example implementation
}
let output = merge(1,"2", true) // number