Skip to content
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

[feature] support union type rest parameter #27532

Closed
4 tasks done
Meir017 opened this issue Oct 3, 2018 · 3 comments
Closed
4 tasks done

[feature] support union type rest parameter #27532

Meir017 opened this issue Oct 3, 2018 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@Meir017
Copy link

Meir017 commented Oct 3, 2018

Search Terms

type, union, rest

Suggestion

allow rest operator on a union type when all cases are array

Use Cases

in order to declare a ctor with overloads, I wrote something like this

type TimeSpanCtorArguments = [number]
    | [number, number, number]
    | [number, number, number, number]
    | [number, number, number, number, number];

class TimeSpan {
    private readonly _ticks: number;
    constructor(...args: TimeSpanCtorArguments) {
        if (typeof args.length === 1) {
            [this._ticks] = args;
        } else if (args.length === 3) {
            const [hours, minutes, seconds] = args;
        } else if (args.length === 4) {
            const [days, hours, minutes, seconds] = args;
        } else {
            const [days, hours, minutes, seconds, milliseconds] = args;
        }
    }
}

I get the error: [ts] A rest parameter must be of an array type.

on the line

    constructor(...args: TimeSpanCtorArguments) {

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 3, 2018
@Meir017
Copy link
Author

Meir017 commented Oct 3, 2018

might be related to #15972

@Meir017
Copy link
Author

Meir017 commented Jan 10, 2019

@RyanCavanaugh any update on this?

@Andarist
Copy link
Contributor

@RyanCavanaugh this already works since 3.3 so the issue can be closed:
TS 3.3 playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants