Title: Suggestion: Upper-bound generic type constraints · Issue #9252 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Upper-bound generic type constraints · Issue #9252 · microsoft/TypeScript
X Title: Suggestion: Upper-bound generic type constraints · Issue #9252 · microsoft/TypeScript
Description: This is a proposal for generic structural supertype constraints. I'll first detail the proposal, and then get into a discussion about use-cases. Proposal A supertype constraint is a constraint on a generic type parameter to be the struct...
Open Graph Description: This is a proposal for generic structural supertype constraints. I'll first detail the proposal, and then get into a discussion about use-cases. Proposal A supertype constraint is a constraint on a...
X Description: This is a proposal for generic structural supertype constraints. I'll first detail the proposal, and then get into a discussion about use-cases. Proposal A supertype constraint is a constraint ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9252
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Upper-bound generic type constraints","articleBody":"This is a proposal for generic structural supertype constraints.\n\nI'll first detail the proposal, and then get into a discussion about use-cases.\n## Proposal\n\nA supertype constraint is a constraint on a generic type parameter to be the structural _supertype_ of another type. It's like a subtype/inheritance constraint, but from the other direction. For example, let's say we have a square:\n\n```\ninterface Square {width : number; height : number}\n```\n\nIn this suggested syntax, you could write:\n\n```\nperformAction\u003cT extended by Square>(partialSquare : T) {}\n```\n\nThis means the type `T` must be a structural supertype of square. So the potential candidates for `T` are:\n\n```\n{width : number}, {height : number}, Square, {}\n```\n\nIt's an established kind of type constraint, not something I just made up. Although it's not exactly common, some languages do implement this feature, such as Scala. In Scala, you can write:\n\n```\ndef performAction[T >: Square](obj : T) = { ... }\n```\n\nTo express a supertype/upper bound constraint. In this case, of course, the constraint isn't structural -- `T` must declare that it implements `Square`. \n## Utility\n\nIn most languages, including Scala, this kind of constraint isn't very useful. It only comes up in certain specific situations. \n\nHowever, Javascript libraries often have this kind of API, where you're allowed to specify the partial properties of an object to modify it (the rest remain at their previous value).\n\nIn many cases, you can support this by having optional interface members, but isn't always possible or correct.\n\nAn important example is React, which defines a method called `setState`:\n\n```\nclass Component {\n var state;\n\n setState(partialState) {\n //merges the properties of partialState with the current state.\n }\n}\n```\n\nThe right signature for this method should be:\n\n```\nclass Component\u003cProps, State> {\n setState\u003cPartialState extended by State>(partialState : PartialState) {\n //merges the properties of partialState with the current state.\n }\n}\n```\n\nCurrently,the definition files state that it is:\n\n```\nsetState(fullState : State) {\n //merges the properties of partialState with the current state.\n}\n```\n\nWhich doesn't fully capture the functionality of the method.\n## Notes\n\nThe suggested syntax doesn't give us a nice way of combining both subtype and supertype constraints. One possibility is:\n\n```\nexampleMethod\u003cT extends LowerBound and extended by UpperBound>\n```\n","author":{"url":"https://github.com/GregRos","@type":"Person","name":"GregRos"},"datePublished":"2016-06-19T09:38:42Z","url":"https://github.com/microsoft/TypeScript/issues/9252"}
| 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:6bb251bf-c782-acb2-904f-2eeaba8e38fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C86E:17CCC0:DDDB5:13673D:6AFBDB0E |
| html-safe-nonce | ff0d61e5bc698a2751aca4a70fb898cedd4e9f867800b4dc602efaf110646df0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODZFOjE3Q0NDMDpERERCNToxMzY3M0Q6NkFGQkRCMEUiLCJ2aXNpdG9yX2lkIjoiMTI5OTU2MjgzMTAzMjM0MzMxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ab5dac331b4a1d9c9607b10abbe38c1d0904bdc4bf3b0b09f748e49c6bcb4560 |
| hovercard-subject-tag | issue:161064954 |
| 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/9252/issue_layout |
| twitter:image | https://opengraph.githubassets.com/75348e33c8bf354ac1667a8a7533625c43ae4b45eebe55c6ee2a1fd0894af7b9/microsoft/TypeScript/issues/9252 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/75348e33c8bf354ac1667a8a7533625c43ae4b45eebe55c6ee2a1fd0894af7b9/microsoft/TypeScript/issues/9252 |
| og:image:alt | This is a proposal for generic structural supertype constraints. I'll first detail the proposal, and then get into a discussion about use-cases. Proposal A supertype constraint is a constraint on a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GregRos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 18742cb872ec73544356e60359266b67aafece70c4ce008042e06227924cdd07 |
| 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 | 820be804f18e1ef3940bd74a00d41d73d02e2b91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width