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

Allow use of spread operator in tuple types #24902

Closed
4 tasks done
aneilbaboo opened this issue Jun 12, 2018 · 5 comments
Closed
4 tasks done

Allow use of spread operator in tuple types #24902

aneilbaboo opened this issue Jun 12, 2018 · 5 comments
Assignees
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@aneilbaboo
Copy link

aneilbaboo commented Jun 12, 2018

Search Terms

tuple type rest spread typescript

Suggestion

I'd like to be able to write tuple types using the spread operator. For example:

type Foo = [string, ...number[]]; // first elt must be string, rest must be numbers

Currently, this produces an error at the ellipsis: "Type expected".

Here is a question asking much the same thing on Stack Overflow: https://stackoverflow.com/questions/44934541/how-to-use-the-spread-operator-in-a-typescript-tuple

It seems related to #10727, but that issue focuses on Object rest/spread, whereas this is specifically about tuples.

Use Cases

Typed tuples are part of the existing language. So it seems natural that one should be able to type rest arguments .

For example, if you want to represent a syntax tree, you might want to write:

type FunCall = ['FUNCALL', Symbol, ...Expression[]]; // error in 2.9.1 :(
type Symbol = string;
type Expression = FunCall | Scalar;
type Scalar = number | string | null;

But this isn't allowed in Typescript 2.9.1.

Examples

type Foo = [string, ...number[]];
const f1: Foo = ["str", 2, 3, 4]; // should pass
const f2: Foo = ["str", "2", 3, 4]; // should fail: 
		// "Type '(string | number)[]' is not assignable to type 'number[]'.
const f2: Foo = ["str", 2, "3", 4]; // should fail:
		// "Type '(number | string)[]' is not assignable to type 'number[]'.
const f1: Foo = [1, 2, 3, 4]; // should fail:
		// "Type 'number' is not assignable to type 'string'.

Checklist

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)
@j-oliveras
Copy link
Contributor

I think is already in progress in #24897 (see Open-ended tuple types block).

@ahejlsberg can answer better.

@aneilbaboo
Copy link
Author

Ah, thanks for the pointer, @j-oliveras.

@ahejlsberg, #24897 seems to deal with rest arguments to functions, whereas this deals with definition of tuple types with the rest operator. Does it makes sense to include tuple types in that issue?

@aneilbaboo
Copy link
Author

Here's another issue which focuses on rest in function calls: #17934

And a work in progress which uses generics for defining tuple types: #17884

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Jun 12, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 12, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jun 12, 2018

Should be implemented by #24897. the syntax will be [number, string*]

@Cryrivers
Copy link

UPDATE: final syntax will be [number, ...string[]].

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants