Title: Allow use of spread operator in tuple types · Issue #24902 · microsoft/TypeScript · GitHub
Open Graph Title: Allow use of spread operator in tuple types · Issue #24902 · microsoft/TypeScript
X Title: Allow use of spread operator in tuple types · Issue #24902 · microsoft/TypeScript
Description: 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, thi...
Open Graph Description: 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 str...
X Description: 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...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/24902
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow use of spread operator in tuple types","articleBody":"## Search Terms\r\n\r\ntuple type rest spread typescript\r\n\r\n## Suggestion\r\n\r\nI'd like to be able to write tuple types using the spread operator. For example:\r\n\r\n```typescript\r\ntype Foo = [string, ...number[]]; // first elt must be string, rest must be numbers\r\n```\r\n\r\nCurrently, this produces an error at the ellipsis: \"Type expected\".\r\n\r\nHere 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\r\n\r\nIt seems related to https://github.com/Microsoft/TypeScript/issues/10727, but that issue focuses on Object rest/spread, whereas this is specifically about tuples.\r\n\r\n## Use Cases\r\n\r\nTyped tuples are part of the existing language. So it seems natural that one should be able to type rest arguments . \r\n\r\nFor example, if you want to represent a syntax tree, you might want to write:\r\n\r\n```typescript\r\ntype FunCall = ['FUNCALL', Symbol, ...Expression[]]; // error in 2.9.1 :(\r\ntype Symbol = string;\r\ntype Expression = FunCall | Scalar;\r\ntype Scalar = number | string | null;\r\n```\r\n\r\nBut this isn't allowed in Typescript 2.9.1.\r\n\r\n## Examples\r\n\r\n```typescript\r\ntype Foo = [string, ...number[]];\r\nconst f1: Foo = [\"str\", 2, 3, 4]; // should pass\r\nconst f2: Foo = [\"str\", \"2\", 3, 4]; // should fail: \r\n\t\t// \"Type '(string | number)[]' is not assignable to type 'number[]'.\r\nconst f2: Foo = [\"str\", 2, \"3\", 4]; // should fail:\r\n\t\t// \"Type '(number | string)[]' is not assignable to type 'number[]'.\r\nconst f1: Foo = [1, 2, 3, 4]; // should fail:\r\n\t\t// \"Type 'number' is not assignable to type 'string'.\r\n```\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [x] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n","author":{"url":"https://github.com/aneilbaboo","@type":"Person","name":"aneilbaboo"},"datePublished":"2018-06-12T13:44:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/24902/TypeScript/issues/24902"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:08ad3f2a-741f-730d-4e01-94090c08f12e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B746:3223CD:A879C6:EB3558:6A61C0BD |
| html-safe-nonce | d5e9535513bf4917f9e227795e16be4a22397866076d389127217b6e44782833 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzQ2OjMyMjNDRDpBODc5QzY6RUIzNTU4OjZBNjFDMEJEIiwidmlzaXRvcl9pZCI6IjQ0MDUxMDkyMTcyMzk4NzU3NzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 36704a321d185d60b45f09e926cf8ba418b2c38b352fef70764a4d167e96a693 |
| hovercard-subject-tag | issue:331594176 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/24902/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f4230d01ed445cd11e32c9315bdc4850cd03cc1b376254c30a413f76a505400a/microsoft/TypeScript/issues/24902 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f4230d01ed445cd11e32c9315bdc4850cd03cc1b376254c30a413f76a505400a/microsoft/TypeScript/issues/24902 |
| og:image:alt | 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 str... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aneilbaboo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6f4633bcf01c1ad14b73fd07dd39ac31d61f3d3c2578ee08ec1792b7b351eeb9 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ac296ae7f21856f1f92adbad22f870b6fbb4b907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width