Title: Type inference lost after spreading array with `ArrayLike` · Issue #58286 · microsoft/TypeScript · GitHub
Open Graph Title: Type inference lost after spreading array with `ArrayLike` · Issue #58286 · microsoft/TypeScript
X Title: Type inference lost after spreading array with `ArrayLike` · Issue #58286 · microsoft/TypeScript
Description: 🔎 Search Terms array inference, type inference, ArrayLike, lodash compact, _.compact 🕗 Version & Regression Information This changed between versions 4.9.5 and 5.0.4 ⏯ Playground Link https://www.typescriptlang.org/play?ts=5.0.4#code/C4T...
Open Graph Description: 🔎 Search Terms array inference, type inference, ArrayLike, lodash compact, _.compact 🕗 Version & Regression Information This changed between versions 4.9.5 and 5.0.4 ⏯ Playground Link https://www.t...
X Description: 🔎 Search Terms array inference, type inference, ArrayLike, lodash compact, _.compact 🕗 Version & Regression Information This changed between versions 4.9.5 and 5.0.4 ⏯ Playground Link https://w...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/58286
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type inference lost after spreading array with `ArrayLike`","articleBody":"### 🔎 Search Terms\r\n\r\narray inference, type inference, ArrayLike, lodash compact, _.compact\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed between versions 4.9.5 and 5.0.4\r\n\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play?ts=5.0.4#code/C4TwDgpgBAsiAq4IB55QgD2BAdgEwGcoBXHAaxwHsB3HAPigF4oBvAKCigFsQBpCEAC4o8ANwduIAGKkAxsACWlHMIAUAQwBOAcwCMw+AEomDAG6UFecQF9xbWcoLAo23BE3rsTKKsPCAgpoeIMhwiJDITpoKONp0DIwM7JyaEMDEmjhQgcEAdABmmpRcqixQADa42sAAFsIArFDWhrlc6mChCEiRwNGx8aqqAPoANFAxeJjGiawSKWkZWcmcKzz8QlAABgAkLBOY1psjc6vScorKalp600knK1Cp6ZniD5zWxw+2Es02dg44JxQLTBAAyCjIEAAkjh8u5cLIIDIcPIlFlmKh0FhcIQSOQqLQ6KoJCCAkF1CBwZDMQAfKA4YjlcpQOmkSb5GIQPAsqD5dTlAjQOkAImFPIADAw6QymTy2RAOTguWw-CIANoAXRMs3mzyymr+bHsjmcIIpVOhsPcqRR0GYZspEMtcJtiORqOUqjVEjKawEwmFugATABmYVjHjui4qHymfnECC3VjWJqfKC9BNQABkWdYknWAYALPUAGzhyRRtFqOPlBNJlgpj4SPkC6A5vN+jbC4tliNnFHR6vxxPahupiS5SeuJUebC+NMAegX6aQ4ytQQgiKglHyFfOaPGRHKlCcEgzbdzvr4-qg3dL5cj+8useH9cbaZbguzl-zN7vvb3AcqxfWsRxmMcm04K8C1vHsH37D0Y1UGs61Hd82A1Qx-hNYFyRAGEXQRJEn3RKBMUwbB8CIUgKBoehiU4Ulsjw2l6UZZlWXwBVOW5OlPyFW8xTpSUeRlDi8XZHiVQMTVRwkJ5FigA02G+Y1AVNPCCPhW1vAdLTXWIoDPW9KDfy7YMwz7Stn2Q180PHThz2-Dtry7OCrJIodQLfBzeX5L922gv93MAxCvNQ8D0M4SdcmndxPAgecJCXFdIDXF1Ui3HdQujQ8oEhMBgDPTRM0Csyi3vDyjKQlCwKSKK-NbZygrcyqcuA2zvPsyCXJg-94Osmq7Mi8dMPEIA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype MyType\u003cT extends unknown> = {\r\n myKey: T;\r\n myFunction: (arg1: T) => void;\r\n};\r\n\r\nconst generate = (): Array\u003cMyType\u003cstring>> => {\r\n return Array.from({ length: 5 }).map\u003cMyType\u003cstring>>((_, index) => {\r\n return {\r\n myKey: `${index}`,\r\n myFunction: (arg1) => {\r\n return;\r\n },\r\n };\r\n });\r\n};\r\n\r\nconst arrayLikeInferenceFunction = \u003cT extends unknown>(\r\n arr: ArrayLike\u003cT | null | undefined | false | \"\" | 0> | null | undefined\r\n): T[] => {\r\n return [];\r\n};\r\n\r\nconst arrayLikeInference = arrayLikeInferenceFunction([\r\n { myKey: \"123\", myFunction: (value) => {} }, // value is correctly inferred as string\r\n true && { myKey: \"456\", myFunction: (value) => {} },\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n ...generate(),\r\n // type inference of myFunction is lost\r\n true && { myKey: \"456\", myFunction: (value) => {} }, // value is inferred as any\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n { myKey: \"456\", myFunction: (value) => {} },\r\n]);\r\n\r\nconst arrayInferenceFunction = \u003cT extends unknown>(\r\n arr: Array\u003cT | null | undefined | false | \"\" | 0> | null | undefined\r\n): T[] => {\r\n return [];\r\n};\r\n\r\nconst arrayInference = arrayInferenceFunction([\r\n { myKey: \"123\", myFunction: (value) => {} }, // value is correctly inferred as string\r\n true && { myKey: \"456\", myFunction: (value) => {} },\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n ...generate(),\r\n // type inference of myFunction is kept\r\n true && { myKey: \"456\", myFunction: (value) => {} }, // value is correctly inferred as string\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n { myKey: \"456\", myFunction: (value) => {} },\r\n]);\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe typings of the arguments of `myFunction` are lost after the array spread when using `ArrayLike`. When using `Array`, the type inference is kept.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe arguments of `myFunction` should be inferred as `string`\r\n\r\n### Additional information about the issue\r\n\r\nI have opened a similar issue in the DefinitelyTyped repository for the lodash typings and they suggested opening an issue in this repository. https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69410\r\n\r\nThe issue originated from using `lodash.compact()` with code similar to the code below:\r\n\r\n```ts\r\nimport _ from \"lodash\";\r\n\r\ntype MyType\u003cT extends unknown> = {\r\n myKey: T;\r\n myFunction: (arg1: T) => void;\r\n};\r\n\r\nconst generate = (): Array\u003cMyType\u003cstring>> => {\r\n return Array.from({ length: 5 }).map\u003cMyType\u003cstring>>((_, index) => {\r\n return {\r\n myKey: `${index}`,\r\n myFunction: (arg1) => {\r\n return;\r\n },\r\n };\r\n });\r\n};\r\n\r\nconst lodashInferred = _.compact([\r\n { myKey: \"123\", myFunction: (value) => {} }, // infers value as string\r\n true && { myKey: \"456\", myFunction: (value) => {} },\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n ...generate(),\r\n { myKey: \"123\", myFunction: (value) => {} }, // infers value as any\r\n true && { myKey: \"456\", myFunction: (value) => {} },\r\n false && { myKey: \"456\", myFunction: (value) => {} },\r\n]);\r\n```","author":{"url":"https://github.com/KarelVerschraegen","@type":"Person","name":"KarelVerschraegen"},"datePublished":"2024-04-22T17:36:47Z","url":"https://github.com/microsoft/TypeScript/issues/58286"}
| 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:cc9bf01b-52e3-28e0-a67f-088bac674d97 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D44:76F02:25D6533:30642E3:6B0189DD |
| html-safe-nonce | 5f7fb47f5228e9f2ab8728d189042548478a197ab83c595a0e3e7eb0a469d01e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDQ0Ojc2RjAyOjI1RDY1MzM6MzA2NDJFMzo2QjAxODlERCIsInZpc2l0b3JfaWQiOiI4NDk3MzEwMzIxMjI3MTcyMzE3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1814574ba1b6639a76eb264d529bd3df8f1739032c923d84997ba1044129c9d5 |
| hovercard-subject-tag | issue:2257114130 |
| 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/58286/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a0dc9536c2cdd5c0e62820d5b9a8827aa826d3afe7bd51817ed5f4f3f1beac7/microsoft/TypeScript/issues/58286 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a0dc9536c2cdd5c0e62820d5b9a8827aa826d3afe7bd51817ed5f4f3f1beac7/microsoft/TypeScript/issues/58286 |
| og:image:alt | 🔎 Search Terms array inference, type inference, ArrayLike, lodash compact, _.compact 🕗 Version & Regression Information This changed between versions 4.9.5 and 5.0.4 ⏯ Playground Link https://www.t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KarelVerschraegen |
| 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