Title: Implicit Symbol.iterator call in for..of loops / spread destructuring doesn't infer `this` generic type parameter · Issue #38388 · microsoft/TypeScript · GitHub
Open Graph Title: Implicit Symbol.iterator call in for..of loops / spread destructuring doesn't infer `this` generic type parameter · Issue #38388 · microsoft/TypeScript
X Title: Implicit Symbol.iterator call in for..of loops / spread destructuring doesn't infer `this` generic type parameter · Issue #38388 · microsoft/TypeScript
Description: TypeScript Version: 4.0.0-dev.20200507 Search Terms: Symbol.iterator type parameter implicit calling for..of loops object spread destructuring this generic Code What follows is 3 different ways of iterating over an object using a custom-...
Open Graph Description: TypeScript Version: 4.0.0-dev.20200507 Search Terms: Symbol.iterator type parameter implicit calling for..of loops object spread destructuring this generic Code What follows is 3 different ways of ...
X Description: TypeScript Version: 4.0.0-dev.20200507 Search Terms: Symbol.iterator type parameter implicit calling for..of loops object spread destructuring this generic Code What follows is 3 different ways of ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38388
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Implicit Symbol.iterator call in for..of loops / spread destructuring doesn't infer `this` generic type parameter","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 4.0.0-dev.20200507\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** Symbol.iterator type parameter implicit calling for..of loops object spread destructuring this generic\r\n\r\n**Code**\r\n\r\nWhat follows is 3 different ways of iterating over an object using a custom-made iterator. However, using `Symbol.iterator` implicitly via the for..of loop fails to produce the same behavior:\r\n\r\n```ts\r\nconst obj = {\r\n\tx: 1,\r\n\ty: 2,\r\n\tz: 3,\r\n\t*[Symbol.iterator]\u003cT extends object>(\r\n\t\tthis: T,\r\n\t): Generator\u003cNonNullable\u003c{ [K in keyof T]: [K, NonNullable\u003cT[K]>] }[keyof T]>, void, unknown> {\r\n\t\tfor (const entry of Object.entries(this)) yield entry as never;\r\n\t},\r\n};\r\n\r\n{\r\n\tconst iter = obj[Symbol.iterator]();\r\n\r\n\tfor (let result = iter.next(); !result.done; result = iter.next()) {\r\n\t\tconst { value } = result;\r\n\t\tvalue; // [\"x\", number] | [\"y\", number] | [\"z\", number]\r\n\t}\r\n}\r\n\r\nfor (const value of obj[Symbol.iterator]()) {\r\n\tvalue; // [\"x\", number] | [\"y\", number] | [\"z\", number]\r\n}\r\n\r\nfor (const value of obj) {\r\n\tvalue; // NonNullable\u003c{ [K in keyof T]: [K, NonNullable\u003cT[K]>]; }[keyof T]>\r\n\t// bad! This should be [\"x\", number] | [\"y\", number] | [\"z\", number]\r\n}\r\n\r\n// also, spread destructuring suffers from the same problem!\r\nconst values = [...obj]; // Array\u003cNonNullable\u003c{ [K in keyof T]: [K, NonNullable\u003cT[K]>]; }[keyof T]>>\r\n// bad! This should be Array\u003c[\"x\", number] | [\"y\", number] | [\"z\", number]>\r\n```\r\n\r\n**Expected behavior:** `for (const value of obj)` should implicitly do the type equivalent of calling our iterator symbol, i.e. `for (const value of obj[Symbol.iterator]())`. That means our type parameter should be correctly inferred like so: `obj[Symbol.iterator]\u003cT>(this: T)`.\r\n\r\n**Actual behavior:** `T` cannot be inferred as the local `this` type, so all derivative types cannot be evaluated and the type stays as its generic version.\r\n\r\n[**Playground Link**](https://www.typescriptlang.org/play/index.html?ts=4.0.0-dev.20200506#code/MYewdgzgLgBCBGArGBeGBvAsAKAJAA8AuGARgBodcBPYgJgrwC9iBmB3AKgG0BlKgW3ggANgDoAllACmAJwCGUEDIC6AHgAqMKfmlgAJhDhIpwKAD4AFJVxQAFuIjF17AJTEA4lLCyFS1QDlwfwBXYWE5eGEpVXQYLgBpGHEwGABrKSoQADMYdWViBLIYQLAQsIiojQTlM2UYAF8udMycvLMigDcQcT0i4LBUsBAAdzAzDGsspRgLUEhYLygZKjgcgHljU1FFmXEpCAs7BxcXGCo94T0tMCWVuUNvDtkAbkp6hnrX7BwsPDnoJLSGSoIyIXgCIRiSQ+RQqCwuL6UKbAixRWAyfahWBoaEyUTeHTw54wACEGIgWNEenAUmJ5KxINx+O0UHhp1+uFw-1gsQ6cmEwSkDRB9OEUC+nL5AtpMAA9LK4gAifCKopgYKCWR1AA+Sqoqpg6s1Khguq4isYBqN8C1bxw9RwOGRM25MClgtWoPBghEEiBvjhJwmeHdMvlSpVao1NpNZsV+qjxp1SstiZjyntjuwztm4ABoc9CEQ7MooeJ4ZKZXCkWisQSSRSzWyuXycXiRUroWrlXU1VqxMaTdaNUo4fgcj0JNy9kMEFsIFCVxtEat0a1pr1q6TG-NqcNa5Ume+2FdocMaC4oivReU5YVAEEZPIqAEgl2KrW2w20hlm3kCu2xRvuUNZVPENS3g0TS-sOZhmDgY4TlO6gzjAc4LpcMDLo+z6qOakb7tucYJoR6Y7haW7pmYQA) \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? --> n/a\r\n","author":{"url":"https://github.com/Validark","@type":"Person","name":"Validark"},"datePublished":"2020-05-07T11:00:06Z","url":"https://github.com/microsoft/TypeScript/issues/38388"}
| 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:1dcca35e-3a7b-ab43-a4e4-d8094966f636 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C500:1F2F:E75CA1:13440B7:6B01BEEC |
| html-safe-nonce | c66f7b6398872c5f75324605916fd907e43ed09bec37c56798846001ab8cce9e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTAwOjFGMkY6RTc1Q0ExOjEzNDQwQjc6NkIwMUJFRUMiLCJ2aXNpdG9yX2lkIjoiNDY5NjE0NDE1NTUwNjc1MzI2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0e5c40c6f33c6d53c9cd8163b4952d454c3154293fb6972f10b149789b3fb400 |
| hovercard-subject-tag | issue:613965564 |
| 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/38388/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4326a3022ca8e8734e480aa8e8bf7b43005ddca32a5f3aed25bf75f30ea2621e/microsoft/TypeScript/issues/38388 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4326a3022ca8e8734e480aa8e8bf7b43005ddca32a5f3aed25bf75f30ea2621e/microsoft/TypeScript/issues/38388 |
| og:image:alt | TypeScript Version: 4.0.0-dev.20200507 Search Terms: Symbol.iterator type parameter implicit calling for..of loops object spread destructuring this generic Code What follows is 3 different ways of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Validark |
| 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