Title: Callback argument type not inferred for union of interfaces · Issue #59309 · microsoft/TypeScript · GitHub
Open Graph Title: Callback argument type not inferred for union of interfaces · Issue #59309 · microsoft/TypeScript
X Title: Callback argument type not inferred for union of interfaces · Issue #59309 · microsoft/TypeScript
Description: 🔎 Search Terms "implicitly has an" inference 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about "inference" ⏯ Playground Link https://www.typescriptlang.org/play/?ta...
Open Graph Description: 🔎 Search Terms "implicitly has an" inference 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about "inference" ⏯ Playground Link...
X Description: 🔎 Search Terms "implicitly has an" inference 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about "inference...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/59309
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Callback argument type not inferred for union of interfaces","articleBody":"### 🔎 Search Terms\r\n\r\n`\"implicitly has an\" inference`\r\n\r\n### 🕗 Version \u0026 Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about \"inference\"\r\n\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?target=5\u0026ts=5.5.3#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgQQGcBPJbAJWEIFcAbGAYU1toCMcBrAHgBUAaOAFE4AXiFQo0AHxwA3gFgAUHBVwAFGigB+AFxC4AHzhI6tAVCp0YuuDwCUegG4QEAEwDcSgL5KloSLCIKGhYuAQkZJQ09ACSqFCYMNC8AuQCwmKCEtJySqrqCKgAtnr8cNjMbJx6RKQUlvRMLOzY3GlCUg5wzm6eij6K-tDwyPGheLWRDTBxaInQAAqSRQiEwClw5DIKyqpqhcAltl1LECtrXFt9A0quwNi0mBZw6NRkMAgQSHBFmGAAMggVjANu0MuJJFApGo8qonlBSgBtAC6fFhKloQMKehMRVYaDRuxUBwSSQRKkm9WiM3i8ygp3O6zKW0M4TqUSss1JyWZ6SkhJOy1W63IKKkfSUvwBWJgakRAEY+AAmPgAZlRcFVAkwEWw6iQdlE23RcAsMGoUG+3wAVNa4ErrnZ3EA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\nexport interface AsyncResultCallback\u003cT, E = Error\u003e {\r\n (err?: E | null, result?: T): void;\r\n}\r\n\r\nexport interface AsyncResultIterator\u003cT, R, E = Error\u003e {\r\n (item: T, callback: AsyncResultCallback\u003cR, E\u003e): void;\r\n}\r\nexport interface AsyncResultIteratorPromise\u003cT, R\u003e {\r\n (item: T): Promise\u003cR\u003e;\r\n}\r\n\r\ndeclare function mapLimit\u003cT, R, E = Error\u003e(\r\n arr: T[],\r\n limit: number,\r\n iterator: AsyncResultIteratorPromise\u003cT, R\u003e | AsyncResultIterator\u003cT, R, E\u003e,\r\n): Promise\u003cR[]\u003e;\r\n\r\nmapLimit([1,2,3], 3, async (n) =\u003e {\r\n return n ** 2;\r\n});\r\n```\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe type of `n` in the `mapLimit` callback is inferred as `any`:\r\n\r\n```\r\nParameter 'n' implicitly has an 'any' type. (7006)\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\nI would expect the type of `n` to be inferred as `number`.\r\n\r\n### Additional information about the issue\r\n\r\nThis example is based on the types for the `async` package:\r\n\r\nhttps://github.com/DefinitelyTyped/DefinitelyTyped/blob/b9820410f85f259cbad6065d8cb6f23f403109ac/types/async/index.d.ts#L321-L325\r\n\r\nNote that if the type for `iterator` in `mapLimit` is changed to either just `AsyncResultIteratorPromise\u003cT, R\u003e` or just `AsyncResultIterator\u003cT, R, E\u003e` (that is, no longer a union type), things work as expected and `n` has type `number`.\r\n\r\nI found a very old discussion of what looks to be the same issue in the `DefinitelyTyped` repo: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/24297. The issue described there is exactly the one I'm facing. One commenter claimed that things worked correctly, and another commenter claimed that changing the way that `async` was imported had some effect? Neither of those sounds plausible to me.\r\n\r\nI found some issues that may describe the same or a similar problem:\r\n\r\n- https://github.com/microsoft/TypeScript/issues/56881\r\n- https://github.com/microsoft/TypeScript/issues/55632\r\n- https://github.com/microsoft/TypeScript/issues/52047\r\n- https://github.com/microsoft/TypeScript/issues/47599\r\n\r\nHowever, my knowledge of TypeScript internals/terminology isn't strong enough to say for sure that this isn't a novel issue I'm facing. If this is in fact a duplicate, please close it as such and I'll add this example to the relevant issue. Thank you!","author":{"url":"https://github.com/nwalters512","@type":"Person","name":"nwalters512"},"datePublished":"2024-07-16T21:16:34.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/59309/TypeScript/issues/59309"}
| 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:a7b386a6-c945-29f8-5a1c-0cb7776d9ebf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA74:FD1F4:4DB0BD:701333:6A61CE5F |
| html-safe-nonce | a2ea767ff7340ef974950bd049bd0420e3085020b6848814018670c208d8dc2f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTc0OkZEMUY0OjREQjBCRDo3MDEzMzM6NkE2MUNFNUYiLCJ2aXNpdG9yX2lkIjoiODI5ODU4NDkwMjAyODM2NTc1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 20451e3601c552afa9bf114359128e0fba3b6faa573f6737a7fa9980cc493f4e |
| hovercard-subject-tag | issue:2412058822 |
| 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/59309/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e69a3ca71a5299965513542c7938c0ec4736debddf3e413925890412359d1e53/microsoft/TypeScript/issues/59309 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e69a3ca71a5299965513542c7938c0ec4736debddf3e413925890412359d1e53/microsoft/TypeScript/issues/59309 |
| og:image:alt | 🔎 Search Terms "implicitly has an" inference 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about "inference" ⏯ Playground Link... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nwalters512 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b2de8c74e5e61e893155ba46ee41bc66170c1644cb795adefa8386d490f7781c |
| 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 | d1866027ded575df8a15c731dd8b9986c9483ceb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width