Skip to content

Function argument spread order with union type tuples #29833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andykais opened this issue Feb 8, 2019 · 1 comment
Closed

Function argument spread order with union type tuples #29833

andykais opened this issue Feb 8, 2019 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@andykais
Copy link

andykais commented Feb 8, 2019

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:

@ahejlsberg ahejlsberg self-assigned this Feb 10, 2019
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Feb 10, 2019
@ahejlsberg ahejlsberg added this to the TypeScript 3.4.0 milestone Feb 10, 2019
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Feb 11, 2019
@andykais
Copy link
Author

😄 Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants