Title: Mapped array/tuple types not always working as expected · Issue #43129 · microsoft/TypeScript · GitHub
Open Graph Title: Mapped array/tuple types not always working as expected · Issue #43129 · microsoft/TypeScript
X Title: Mapped array/tuple types not always working as expected · Issue #43129 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms Mapped array types 🕗 Version & Regression Information I noticed now in TS 4.1.2. ⏯ Code and Playground Links There are two problems. The following has an error in the mapped type despite that I did it just like ...
Open Graph Description: Bug Report 🔎 Search Terms Mapped array types 🕗 Version & Regression Information I noticed now in TS 4.1.2. ⏯ Code and Playground Links There are two problems. The following has an error in the mapp...
X Description: Bug Report 🔎 Search Terms Mapped array types 🕗 Version & Regression Information I noticed now in TS 4.1.2. ⏯ Code and Playground Links There are two problems. The following has an error in the ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43129
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mapped array/tuple types not always working as expected","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nMapped array types\r\n\r\n### 🕗 Version \u0026 Regression Information\r\n\r\nI noticed now in TS 4.1.2.\r\n\r\n### ⏯ Code and Playground Links\r\n\r\nThere are two problems.\r\n\r\nThe following has an error in the mapped type despite that I did it just like in https://github.com/microsoft/TypeScript/pull/26063 (as far as I know):\r\n\r\n```ts\r\ntype Constructor\u003cT = object, A extends any[] = any[], Static = {}\u003e = (new (...a: A) =\u003e T) \u0026 Static\r\n\r\ntype ElementTypeArrayToInstArray\u003cT extends Constructor[]\u003e = {\r\n\t[K in keyof T]: InstanceType\u003cT[K]\u003e // \u003c----------- ERROR\r\n}\r\ntype ArrayValues\u003cT extends any[]\u003e = T[keyof T]\r\ntype ElementTypes\u003cT extends Constructor[]\u003e = ArrayValues\u003cElementTypeArrayToInstArray\u003cT\u003e\u003e\r\n\r\ntype test = ElementTypes\u003c[typeof HTMLAnchorElement, typeof HTMLDivElement]\u003e\r\n```\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFHAEYBWE2ANFAIJQQAewECAJklAIYIgA2gF0SfASPoBlYL2ABLTGIDeAXwB8YgBQIIAdyhaAdCd4AuRgEoSmgtYBkUGXMUAoV6EhQAogBsIALYcwATgEAyoqLwgBHAAksjAEVEghMxsHNywiCgY2LgimqTKrgCQggDSUPIIUADWECBwAGZQBMIWCSj8mBChkISVwpoA9CNQeAC00zOzkz4ASgsA8guuqh5hjJHRAGq8vugQSGms7Fw8-ELDYgSCDU2t7ZtefoHB-cenGRfZiXk4VCFMTJPYHI4nN5BBAhMKgmLxRLwwjqdTuTzQdgoMRQj5hE6CDEtKAACQIAFkADIMBCYAAWuFxMPoRNaZKpABF5AA3JnAYauIA)\r\n\r\nBut notice that when you hover on `test`, it is a union of all the values of the array including values from non-numeric keys, which is unlike the examples in #26063.\r\n\r\nThere seems to some sort of special casing happening.\r\n\r\nI had to specify that I want `number` keys only in order(unlike #26063) to get rid of the error:\r\n\r\n```js\r\ntype Constructor\u003cT = object, A extends any[] = any[], Static = {}\u003e = (new (...a: A) =\u003e T) \u0026 Static\r\n\r\ntype ElementTypeArrayToInstArray\u003cT extends Constructor[]\u003e = {\r\n\t[K in keyof T]: InstanceType\u003cT[number \u0026 K]\u003e // \u003c----------- GOOD\r\n}\r\ntype ArrayValues\u003cT extends any[]\u003e = T[keyof T]\r\ntype ElementTypes\u003cT extends Constructor[]\u003e = ArrayValues\u003cElementTypeArrayToInstArray\u003cT\u003e\u003e\r\n\r\ntype test = ElementTypes\u003c[typeof HTMLAnchorElement, typeof HTMLDivElement]\u003e\r\n```\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFHAEYBWE2ANFAIJQQAewECAJklAIYIgA2gF0SfASPoBlYL2ABLTGIDeAXwB8YgBQIIAdyhaAdCd4AuRgEoSmgtYBkUGXMUAoV6EhQAogBsIALYcwATgEAyoqLwgBHAAksjAEVEghMxsHNywiCgY2LgimqTKrgCQggDSUPIIUADWECBwAGZQBMIWCSj8mBChkISCCOgB5BCoUI4VwpoA9LNQeAC0K6trS1AA4gDy2wAirqoeYYyR0QBqvL7oEEhprOxcPPxCM2IEgg1Nre3HXn6BYL9W73DJPbKJPI4VCFMTJC5XG53AFBBAhMLwmLxRKYwjqdTuTzQdgoMQooFhO6CIktKAACQIAFkADIMBCYAAWuHJaPoNNaDJZe3kADcecAZq4gA)\r\n\r\n\r\nBut notice that `test` still contains all the values of all keys, not just array values.\r\n\r\nFinally, I had to specify numeric keys for `ArrayValues`:\r\n\r\n```ts\r\ntype Constructor\u003cT = object, A extends any[] = any[], Static = {}\u003e = (new (...a: A) =\u003e T) \u0026 Static\r\n\r\ntype ElementTypeArrayToInstArray\u003cT extends Constructor[]\u003e = {\r\n\t[K in keyof T]: InstanceType\u003cT[number \u0026 K]\u003e\r\n}\r\ntype ArrayValues\u003cT extends any[]\u003e = T[number \u0026 keyof T] // \u003c-------- number\r\ntype ElementTypes\u003cT extends Constructor[]\u003e = ArrayValues\u003cElementTypeArrayToInstArray\u003cT\u003e\u003e\r\n\r\ntype test = ElementTypes\u003c[typeof HTMLAnchorElement, typeof HTMLDivElement]\u003e\r\n```\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFHAEYBWE2ANFAIJQQAewECAJklAIYIgA2gF0SfASPoBlYL2ABLTGIDeAXwB8YgBQIIAdyhaAdCd4AuRgEoSmgtYBkUGXMUAoV6EhQAogBsIALYcwATgEAyoqLwgBHAAksjAEVEghMxsHNywiCgY2LgimqTKrgCQggDSUPIIUADWECBwAGZQBMIWCSj8mBChkISCCOgB5BCoUI4Vwuquqh5hjJHRAGq8vugQSGms7Fw8-EIzYgRDI2MTjg1Nre1QAPT3UHgAtG-vb1DDo+MLXn6BYL9LY7DL7bKJPI4VCFMTJVbrTbbAFBBAhMLwmLxRKYwjqWZ-aDsFBiFFAsLbQSeCAtKAACQIAFkADIMBCYAAWuDJaPo1NpDJZABF5AA3HnAGauIA)\r\n\r\nBut notice if I use `Promise` instead of `InstanceType`, the first issues goes away, although `number` is still needed in `ArrayValues`:\r\n\r\n```ts\r\ntype Constructor\u003cT = object, A extends any[] = any[], Static = {}\u003e = (new (...a: A) =\u003e T) \u0026 Static\r\n\r\ntype ElementTypeArrayToInstArray\u003cT extends Constructor[]\u003e = {\r\n\t[K in keyof T]: Promise\u003cT[K]\u003e // \u003c-------- no number\r\n}\r\ntype ArrayValues\u003cT extends any[]\u003e = T[number \u0026 keyof T] // \u003c-------- number still needed\r\ntype ElementTypes\u003cT extends Constructor[]\u003e = ArrayValues\u003cElementTypeArrayToInstArray\u003cT\u003e\u003e\r\n\r\ntype test = ElementTypes\u003c[typeof HTMLAnchorElement, typeof HTMLDivElement]\u003e\r\n```\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFHAEYBWE2ANFAIJQQAewECAJklAIYIgA2gF0SfASPoBlYL2ABLTGIDeAXwB8YgBQIIAdyhaAdCd4AuRgEoSmgtYBkUGXMUAoV6EhQAogBsIALYcwATgEAyoqLwgBHAAksjAEVEghMxsHNywiCgY2LgimqTKrgCQggDSUPIIUADWECBwAGZQBMIWAAqocAHySBCElcKaAPSjUHgAtDOzM1AIcAvoAeQQqK6qHmGMkdEAary+6BBIaazsXDz8QiNiBIIIK2uoUI4NTa3tUOOTc3PLVbrKAoeS+XwLCAQTjQ7ZePyBYKhSBnIgXTI8eCJPI4VCFMTJA5HE5nBFBBAhMKEmLxRLUwjqdTuTzQdgoMRkpFhM6CFktKAACQIAFkADIMBCYAAWuE5FPofNaQrFABF5AA3OXAEauIA)\r\n\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nDoes not work like #26063\r\n\r\n### 🙂 Expected behavior\r\n\r\nWorks like #26063\r\n\r\n---\r\n\r\nThere seems to be some sort of special casing going on.","author":{"url":"https://github.com/trusktr","@type":"Person","name":"trusktr"},"datePublished":"2021-03-07T21:58:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/43129/TypeScript/issues/43129"}
| 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:ecc0da9a-77e5-61c0-f8c5-fc04facb36b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D714:21F886:148162:1DE964:6A61FB91 |
| html-safe-nonce | ef3fb37be305d19dbc08ef32199024a6d4ab519fed1854558d767314fb0819ea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzE0OjIxRjg4NjoxNDgxNjI6MURFOTY0OjZBNjFGQjkxIiwidmlzaXRvcl9pZCI6IjIxMjYxMTg0MjA4NzAyMDAyMDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7ac5bb7a2337bb393f26baf33a0e478c02db7b225bcdbf5321b1bc7bbc04b790 |
| hovercard-subject-tag | issue:824031972 |
| 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/43129/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3529cfdfdf858aa571ac8d81b6c5acafa3c3ae7d2aa5842200d36dfc328f093f/microsoft/TypeScript/issues/43129 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3529cfdfdf858aa571ac8d81b6c5acafa3c3ae7d2aa5842200d36dfc328f093f/microsoft/TypeScript/issues/43129 |
| og:image:alt | Bug Report 🔎 Search Terms Mapped array types 🕗 Version & Regression Information I noticed now in TS 4.1.2. ⏯ Code and Playground Links There are two problems. The following has an error in the mapp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trusktr |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9e41a6fd863e96b9c7e9185dc317bf107dbbf8cb10efd6dea957211963f33775 |
| 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 | 2ed5e9c44acb35dcc1d752e449d2a6f7a6164b7b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width