Title: Generic type parameter problem/limitation/question · Issue #4967 · microsoft/TypeScript · GitHub
Open Graph Title: Generic type parameter problem/limitation/question · Issue #4967 · microsoft/TypeScript
X Title: Generic type parameter problem/limitation/question · Issue #4967 · microsoft/TypeScript
Description: Consider the following which is a modified cut-down version of the immutable-js type definition: interface IIterable
Open Graph Description: Consider the following which is a modified cut-down version of the immutable-js type definition: interface IIterable
X Description: Consider the following which is a modified cut-down version of the immutable-js type definition: interface IIterable<T, K, V> { has(key: K): boolean; includes(value: V): boolean; slice(begin?...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/4967
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic type parameter problem/limitation/question","articleBody":"Consider the following which is a modified cut-down version of the [`immutable-js`](https://github.com/facebook/immutable-js/blob/59028b5e8552339acbdaab0c3f6547bd131f8f54/dist/immutable.d.ts) type definition:\n\n``` typescript\ninterface IIterable\u003cT, K, V> {\n has(key: K): boolean;\n includes(value: V): boolean;\n slice(begin?: number, end?: number): T\n // flatMap\u003cMK, MV>(...): Problems here!\n}\n\ninterface ISeq\u003cT, K, V> extends IIterable\u003cT, K, V> {\n toSeq(): T;\n}\n\ninterface IKeyedIterable\u003cT, K, V> extends IIterable\u003cT, K, V> {\n // flip(): Problems here!\n}\n\ninterface List\u003cV> extends IIterable\u003cList\u003cV>, number, V> {\n set(index: number, value: V): List\u003cV>;\n}\n\ninterface KeyedSeq\u003cK, V> extends ISeq\u003cKeyedSeq\u003cK, V>, K, V>, IKeyedIterable\u003cKeyedSeq\u003cK, V>, K, V> {\n}\n```\n\nThe existence of the type parameter `T` for the definitions of `IIterable` and `ISeq` allows `slice()` to reflect that it returns a sliced instance of the same container 'type' without requiring a type assertion:\n\n``` typescript\nlet l1: List\u003cstring>;\nlet l2: List\u003cstring> = l1.slice(0, 2);\n```\n\nHowever I can't type `flatMap` and `flip`. Taking flip as an example, it should return a container of the same 'type' but with `K` and `V` switched. `flatMap` should return a container of the same 'type' but with `MK` and `MV`.\n\nThis problem would be 'solved' if we could refer to generic types without requiring that they be passed arguments:\n\n``` typescript\ninterface IIterable\u003cT, K, V> {\n has(key: K): boolean;\n includes(value: V): boolean;\n slice(begin?: number, end?: number): T\u003cK, V>\n flatMap\u003cMK, MV>(mapper: (value?: V, key?: K, iter?: T\u003cK, V>) => T\u003cMK, MV>, context?: any): T\u003cMK, MV>\n}\n\ninterface ISeq\u003cT, K, V> extends IIterable\u003cT, K, V> {\n toSeq(): T\u003cK, V>;\n}\n\ninterface IKeyedIterable\u003cT, K, V> extends IIterable\u003cT, K, V> {\n flip(): T\u003cV, K>;\n}\n\ninterface List\u003cV> extends IIterable\u003cList, number, V> {\n set(index: number, value: V): List\u003cV>;\n}\n\ninterface KeyedSeq\u003cK, V> extends ISeq\u003cKeyedSeq, K, V>, IKeyedIterable\u003cKeyedSeq, K, V> {\n}\n```\n\nAm I missing something obvious here? \n\nOr do I need to look to some higher order definition with code generation of (generic) interfaces to properly support this?\n","author":{"url":"https://github.com/myitcv","@type":"Person","name":"myitcv"},"datePublished":"2015-09-25T12:08:47Z","url":"https://github.com/microsoft/TypeScript/issues/4967"}
| 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:3a87b310-a1b8-78d0-6ccc-5ad38afc4be6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA64:21BD98:5E833B:7759FE:6B011218 |
| html-safe-nonce | f5af9629d7da1347a422a13fcd76ee0dffb57a0dcf63020873c238b06b0deedd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTY0OjIxQkQ5ODo1RTgzM0I6Nzc1OUZFOjZCMDExMjE4IiwidmlzaXRvcl9pZCI6IjQxNjUxODA5OTI0OTcxOTM0OTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ff35e77a1f774195f8f39f31f8890004cb691133980fc39bd8036c89fbdaa219 |
| hovercard-subject-tag | issue:108312685 |
| 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/4967/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a48c0c5aeaa601e16368b15efeb7926eb74f06f9517268d091b748049e27b0f1/microsoft/TypeScript/issues/4967 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a48c0c5aeaa601e16368b15efeb7926eb74f06f9517268d091b748049e27b0f1/microsoft/TypeScript/issues/4967 |
| og:image:alt | Consider the following which is a modified cut-down version of the immutable-js type definition: interface IIterable |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | myitcv |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width