Title: TSServer hangs when using recursive types on tuples and pressing "," · Issue #26155 · microsoft/TypeScript · GitHub
Open Graph Title: TSServer hangs when using recursive types on tuples and pressing "," · Issue #26155 · microsoft/TypeScript
X Title: TSServer hangs when using recursive types on tuples and pressing "," · Issue #26155 · microsoft/TypeScript
Description: Disclaimer: This is probably not intended use of the type system but it might show an underlying issue so I still report it. TypeScript Version: 3.1.0-dev.20180802 Search Terms: recursive conditional type tuple freeze Code While experime...
Open Graph Description: Disclaimer: This is probably not intended use of the type system but it might show an underlying issue so I still report it. TypeScript Version: 3.1.0-dev.20180802 Search Terms: recursive condition...
X Description: Disclaimer: This is probably not intended use of the type system but it might show an underlying issue so I still report it. TypeScript Version: 3.1.0-dev.20180802 Search Terms: recursive condition...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26155
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TSServer hangs when using recursive types on tuples and pressing \",\"","articleBody":"**Disclaimer:** This is probably not intended use of the type system but it might show an underlying issue so I still report it.\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.1.0-dev.20180802\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** recursive conditional type tuple freeze\r\n\r\n**Code**\r\nWhile experimenting with the examples found in https://github.com/Microsoft/TypeScript/pull/24897#issuecomment-401401470, I noticed some weird behavior when it comes to recursive types. Apparently tail recursion on type members seems to work, which means it can be used to create crazy types like `Reverse`:\r\n```ts\r\n/**\r\n * Returns the first item's type in a tuple\r\n */\r\nexport type Head\u003cT extends any[]> =\r\n\tT extends [infer H, ...any[]] ? H : never;\r\n\r\n/**\r\n * Returns all but the first item's type in a tuple/array\r\n */\r\nexport type Tail\u003cT extends any[]> =\r\n\t((...args: T) => any) extends ((head: any, ...tail: infer R) => any) ? R : never;\r\n\r\n/**\r\n * Returns the given tuple/array with the item type prepended to it\r\n */\r\nexport type Unshift\u003cList extends any[], Item> =\r\n\t((first: Item, ...rest: List) => any) extends ((...list: infer R) => any) ? R : never;\r\n\r\n/** Reverses the given list */\r\nexport type Reverse\u003cList extends any[]> = _Reverse\u003cList, []>;\r\n\r\nexport type _Reverse\u003cSource extends any[], Result extends any[] = []> = {\r\n\t// If the source list is empty, return the result\r\n\t1: Result,\r\n\t// else prepend the head of source to the result list and\r\n\t// continue recursion with the tail of the source\r\n\t0: _Reverse\u003cTail\u003cSource>, Unshift\u003cResult, Head\u003cSource>>>,\r\n}[Source extends [] ? 1 : 0];\r\n```\r\nWhen opening a file where that type is used, VSCode's Intellisense shows the correct type after a minor delay:\r\n```ts\r\ntype Foo = Reverse\u003c[1, 2, 3]>; // [3, 2, 1]\r\nconst f: Foo = [2, 1]; // error: 2 is not assignable to 3\r\n```\r\nAfter adding a comma after the 3 in the recursive type, TSServers comes to a halt (1 core with full CPU usage).\r\n```ts\r\ntype Foo = Reverse\u003c[1, 2, 3,]>; // FREEZE\r\n```\r\nHowever, if I paste a comma and a number from somewhere else in a single operation, it works **just fine**:\r\n```ts\r\ntype Foo = Reverse\u003c[1, 2, 3,4]>; // \",4\" pasted from somewhere => all good!\r\n```\r\nThe same thing happens when changing a type to this recursive type\r\n```ts\r\n// start here\r\ntype Foo = [1, 2, 3];\r\n// change to this:\r\ntype Foo = Reverse\u003c[1, 2, 3]; // broken\r\n```\r\nbut not doing it in this order:\r\n```ts\r\n// start here\r\ntype Foo = [1, 2, 3];\r\n// change to this:\r\ntype Foo = \u003c[1, 2, 3]>;\r\n// then to this:\r\ntype Foo = Reverse\u003c[1, 2, 3]>; // all good!\r\n```\r\n\r\n**Related Issues:** #24897\r\n","author":{"url":"https://github.com/AlCalzone","@type":"Person","name":"AlCalzone"},"datePublished":"2018-08-02T13:05:26Z","url":"https://github.com/microsoft/TypeScript/issues/26155"}
| 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:8277c339-7b0d-1bfb-22b4-9bc164f769c1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD90:1F65:20A7EE7:298A722:6B01CA7E |
| html-safe-nonce | 00d61537ceb08bbab31513cb78fe740ff1a05b58859d96d5f0080645d616905b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDkwOjFGNjU6MjBBN0VFNzoyOThBNzIyOjZCMDFDQTdFIiwidmlzaXRvcl9pZCI6IjgzODg5NTAxNzMyNDk4MTcyMTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f5fa258f92c6b84ac1d9220f07a3030d47a023bab82a33867dfe7439fadcff09 |
| hovercard-subject-tag | issue:347003177 |
| 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/26155/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c7c8baf7a7a3ab14182c22a3bd18db4dc4e103e5bda68a5f919229c8ef1d0025/microsoft/TypeScript/issues/26155 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c7c8baf7a7a3ab14182c22a3bd18db4dc4e103e5bda68a5f919229c8ef1d0025/microsoft/TypeScript/issues/26155 |
| og:image:alt | Disclaimer: This is probably not intended use of the type system but it might show an underlying issue so I still report it. TypeScript Version: 3.1.0-dev.20180802 Search Terms: recursive condition... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlCalzone |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width