Title: suggestion: explicit "tuple" syntax · Issue #16656 · microsoft/TypeScript · GitHub
Open Graph Title: suggestion: explicit "tuple" syntax · Issue #16656 · microsoft/TypeScript
X Title: suggestion: explicit "tuple" syntax · Issue #16656 · microsoft/TypeScript
Description: Problem I'm writing this after encountering (what I think is) #3369. I'm a noob to TS, so I apologize for any misunderstandings on my part. The behavior the lack of type inference here: interface Foo { bar: [number, number]; } interface ...
Open Graph Description: Problem I'm writing this after encountering (what I think is) #3369. I'm a noob to TS, so I apologize for any misunderstandings on my part. The behavior the lack of type inference here: interface F...
X Description: Problem I'm writing this after encountering (what I think is) #3369. I'm a noob to TS, so I apologize for any misunderstandings on my part. The behavior the lack of type inference here: int...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/16656
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"suggestion: explicit \"tuple\" syntax","articleBody":"### Problem\r\n\r\nI'm writing this after encountering (what I think is) #3369. I'm a noob to TS, so I apologize for any misunderstandings on my part. The behavior the lack of type inference here:\r\n\r\n```typescript\r\ninterface Foo {\r\n bar: [number, number];\r\n}\r\n\r\ninterface McBean {\r\n baz: Foo;\r\n}\r\n\r\n// error: McMonkey does not implement McBean\r\nclass McMonkey implements McBean {\r\n baz = {\r\n bar: [0, 1]\r\n };\r\n}\r\n\r\n// vs\r\n\r\n// no error\r\nclass McMonkey implements McBean {\r\n baz: Foo = {\r\n bar: [0, 1]\r\n };\r\n}\r\n```\r\n\r\nBecause array literals are (correctly) inferred to be arrays, TS is limited in its ability to infer \"tuple\". This means there's an added overhead to working with tuples, and discourages use. \r\n\r\nAs I see it, the problem is that a tuple is defined using array literal notation.\r\n\r\n### A Conservative Solution\r\n\r\nAdding a type query (?) such as `tuple` (.e.g `tuple`) would be better than nothing:\r\n\r\n```typescript\r\nclass McMonkey implements McBean {\r\n baz = {\r\n bar: \u003ctuple [number,number]> [0, 1]\r\n };\r\n}\r\n```\r\n\r\n...but this is still clunky, because you'd have to use it just as much as you'd have to explicitly declare the type.\r\n\r\n### A Radical Solution\r\n\r\nThere's a precedent (Python) for a tuple syntax of `(x, y)`. Use it:\r\n\r\n```typescript\r\ninterface Foo {\r\n bar: (number, number);\r\n}\r\n\r\ninterface McBean {\r\n baz: Foo;\r\n}\r\n\r\nclass McMonkey implements McBean {\r\n baz = {\r\n bar: (0, 1)\r\n };\r\n}\r\n```\r\n\r\n#### Obvious Problem\r\n\r\nThe [comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) is a thing, so `const oops = 0, 1` is valid JavaScript. `0` is just a noop, and the value of `oops` will be `1`. Python does not have a comma operator (which is meaningful in itself). \r\n\r\nI've occasionally used the comma operator in a `for` loop, similar to the MDN article. Declaring `var`'s at the top of a function is/was common:\r\n\r\n```js\r\nfunction () {\r\n var a, b, c;\r\n}\r\n```\r\n\r\nParens are of course used in the syntax of loops and conditionals, as well as a means of grouping expressions.\r\n\r\nA nuance of Python's tuples are that they *must* contain at one or more commas, which *could* help: \r\n\r\n```python\r\nfoo = (0) # syntax error or just `0`; can't recall\r\nfoo = (0,) # valid tuple\r\n```\r\n\r\n...or it could actually make matters worse, because `(0, )` is an unterminated statement in JavaScript.\r\n\r\nThat all being said, using the suggested Python-like syntax, it seems *difficult* but *possible* for the compiler to understand when the code means \"tuple\" vs. when it doesn't. \r\n\r\n* * *\r\n\r\nI'd be behind any other idea that'd achieve the same end. I don't know how far TS is willing to diverge from JS, and I imagine significant diversions such as the above are not taken lightly.\r\n\r\n(I apologize if something like this has been proposed before; I searched suggestions but didn't find what I was looking for)","author":{"url":"https://github.com/boneskull","@type":"Person","name":"boneskull"},"datePublished":"2017-06-20T19:25:17Z","url":"https://github.com/microsoft/TypeScript/issues/16656"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:2bdf298a-e740-fba3-d61d-231751300fa8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C046:37820D:BE4266:FF0142:6B00E9A0 |
| html-safe-nonce | 024970f49e9e6742abfe1b7c0b09b3d6486dc56e6f88954ba8c6cd33794c0267 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDQ2OjM3ODIwRDpCRTQyNjY6RkYwMTQyOjZCMDBFOUEwIiwidmlzaXRvcl9pZCI6IjI3NDk3MjA0NzIyMzE5MzAyNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 110830ec868d0887fc23a55eb702b204a1225b4dc66ae7b61378ba81abda8c13 |
| hovercard-subject-tag | issue:237318127 |
| 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/16656/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0f01355b7cae04eec089985ca6b1afea3e0dff26fcfb81d1c6fe631734f8ce59/microsoft/TypeScript/issues/16656 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0f01355b7cae04eec089985ca6b1afea3e0dff26fcfb81d1c6fe631734f8ce59/microsoft/TypeScript/issues/16656 |
| og:image:alt | Problem I'm writing this after encountering (what I think is) #3369. I'm a noob to TS, so I apologize for any misunderstandings on my part. The behavior the lack of type inference here: interface F... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | boneskull |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width