Skip to content

Function argument spread order with union type tuples #29833

Closed
@andykais

Description

@andykais

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:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions