Closed
Description
Apologies if this is a feature and not a bug, but it seems like a bug to me, since you can achieve the desired behavior by writing more runtime code.
TypeScript Version: 3.3.3 (still true in 3.4.0-dev.20190208)
Search Terms:
function argument spread order with union type tuples
Code
type ArgsUnion = [number, string] | [number, Error]
type TupleUnionFunc = (...params: ArgsUnion) => number
// arguments are a union of tuples
const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
// (parameter) num: string | number | Error
return num // fails
}
const funcUnionTupleRest: TupleUnionFunc = (...params) => {
const [num, strOrErr] = params
return num // succeeds!
}
Expected behavior:
num
variable should preserve order of tuple types
const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
// (parameter) num: number
return num // succeeds
}
Actual behavior:
funcUnionTupleNoRest
does not preserve order of rest parameters on tuple
const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
// (parameter) num: string | number | Error
return num // fails
}
There is a workaround to this by using function overrides instead of union argument types, though they are less convenient because on each function declaration, each parameter needs to be typed individually
Playground Link: https://www.typescriptlang.org/play/index.html#.....
Related Issues: