Title: Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. · Issue #30728 · microsoft/TypeScript · GitHub
Open Graph Title: Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. · Issue #30728 · microsoft/TypeScript
X Title: Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. · Issue #30728 · microsoft/TypeScript
Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. · Issue #30728 · microsoft/TypeScript
Open Graph Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. ·...
X Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type '...
Opengraph URL: https://github.com/microsoft/TypeScript
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'.","articleBody":"**TypeScript Version:** 3.4.0-dev.20190403\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction toHTML\u003cT, K extends keyof T>(items: T[], key: T[K] extends T[] ? K : never) {\r\n if (!items.length) return '';\r\n // i[key] here is an error :(\r\n const innerHTML = items.map(i => `\u003cli>${i}${toHTML(i[key], key)}\u003c/li>`).join('\\n');\r\n return `\u003cul>\\n${innerHTML}\\n\u003c/ul>`;\r\n}\r\n\r\nclass Item {\r\n constructor(public name: string, public nodes: Item[]) { }\r\n toString() { return this.name; }\r\n}\r\n\r\nconst tree = [\r\n new Item(\"Root\", [\r\n new Item(\"Node 1\", [new Item(\"Subnode 1\", [])]),\r\n new Item(\"Node 2\", [])\r\n ])\r\n];\r\n\r\n// This works correctly! Anything but 'nodes' (eg. 'name') is an error.\r\ndocument.write(toHTML(tree, 'nodes'));\r\n```\r\n\r\nIdea: Take a list of type `T` and a key `K` into type `T` that returns a `T[]` and recursively walk the entire tree.\r\n\r\n**Expected behavior:**\r\n\r\nIf I specify that a `keyof T` must extend `T[]` (or it becomes `never`), I can use it as such.\r\n\r\n**Actual behavior:**\r\n\r\nIt partially works, but indexing into the `T` type with `K` shows the error:\r\n\r\n```\r\ntest.ts:4:56 - error TS2345: Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'.\r\n\r\n4 const innerHTML = items.map(i => `\u003cli>${i}${toHTML(i[key], key)}\u003c/li>`).join('\\n');\r\n ~~~~~~\r\n```\r\n\r\n**Playground Link:** http://www.typescriptlang.org/play/#src=function%20mapTree%3CT%2C%20K%20extends%20keyof%20T%2C%20R%3E(nodes%3A%20T%5B%5D%2C%20childrenKey%3A%20T%5BK%5D%20extends%20(T%5B%5D%20%7C%20undefined)%20%3F%20K%20%3A%20never%2C%20fn%3A%20(item%3A%20T)%20%3D%3E%20R)%3A%20R%5B%5D%20%7B%0D%0A%20%20%20%20const%20result%3A%20R%5B%5D%20%3D%20%5B%5D%0D%0A%20%20%20%20for%20(const%20node%20of%20nodes)%20%7B%0D%0A%20%20%20%20%20%20%20%20fn(node)%0D%0A%20%20%20%20%20%20%20%20const%20children%20%3D%20node%5BchildrenKey%5D%0D%0A%20%20%20%20%20%20%20%20if%20(children)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20This%20does%20not%20work%20even%20if%20we%20validate%20that%20T%5BK%5D%20is%20always%20T%5B%5D.%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20result.push(...mapTree(children%2C%20childrenKey%2C%20fn))%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%20result%0D%0A%7D%0D%0A%0D%0Ainterface%20MenuItem%20%7B%0D%0A%20%20%20%20id%3A%20number%0D%0A%20%20%20%20label%3A%20string%0D%0A%20%20%20%20submenu%3F%3A%20MenuItem%5B%5D%0D%0A%7D%0D%0A%0D%0Aconst%20menu%3A%20MenuItem%5B%5D%20%3D%20%5B%5D%0D%0A%2F%2F%20This%20works%3A%20changing%20submenu%20to%20something%20else%20is%20an%20error.%0D%0Aconst%20allLabels%20%3D%20mapTree(menu%2C%20%22submenu%22%2C%20item%20%3D%3E%20item.label)%0D%0A\r\n","author":{"url":"https://github.com/blixt","@type":"Person","name":"blixt"},"datePublished":"2019-04-03T09:39:47Z","url":"https://github.com/microsoft/TypeScript/issues/30728"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:4712efd2-8d0b-a1ff-1b06-3e4166b59ad6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED50:3D97C8:2A0BB35:37FEC48:6B01BD1E |
| html-safe-nonce | 835c476a21496b1ea7f0a258a6cc4e9b9f912d36837abecb4b3a300d2306b764 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDUwOjNEOTdDODoyQTBCQjM1OjM3RkVDNDg6NkIwMUJEMUUiLCJ2aXNpdG9yX2lkIjoiNDg4Njk4OTg2NzY2MzM0Mjg3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | efd65b7507dca42ff4761e43076cc02c1536e2292fa6398ef63a40aad3a03a25 |
| hovercard-subject-tag | repository:20929025 |
| 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/microsoft/TypeScript/issues/30728 |
| twitter:image | https://opengraph.githubassets.com/47cde802d3847ff7ce61448bd4f8c3272d012c9a3f2b9085b71a67530bf7bbd6/microsoft/TypeScript |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/47cde802d3847ff7ce61448bd4f8c3272d012c9a3f2b9085b71a67530bf7bbd6/microsoft/TypeScript |
| og:image:alt | TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Argument of type 'T[T[K] extends T[] ? K : never]' is not assignable to parameter of type 'T[]'. ·... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-cache |
| 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