I have a generic function lets call it func. Each argument in the function is of type string | [string, Function] . For simplicity the func is returning the argument array as it is.
function func <T extends (string | [string, Function])[]>(...args: T): T {
return args
}
const re = func('a', ['b', () => {}])
I call func('a', ['b', () => {}], 'c') the return type I get is ['a', [string, () => void], 'c'] What I want is ['a', ['b', () => void], 'c']. For the 1st and 3rd argument, the type is string literal but the second argument which is tuple, it fails to infer the string literal b and instead it is string.
// not working
function func <T extends (string | readonly [string, Function])[]>(...args: T): T {
return args
}
// not working
function func <T extends readonly (string | [string, Function])[]>(...args: T): T {
return args
}
It works, if I use const with second argument, but then it defeats the purpose.
// working but
const re = func('a', ['b', () => {}] as const)
So, is there any way, this can be achieved without specifying the argument as const . I have tried using readonly in the function type definition, but still the same inference.