Title: Sub-type type · Issue #6218 · microsoft/TypeScript · GitHub
Open Graph Title: Sub-type type · Issue #6218 · microsoft/TypeScript
X Title: Sub-type type · Issue #6218 · microsoft/TypeScript
Description: I have two use cases that I cannot seem to type correctly in TypeScript. Both use cases involve functions that take a parameter of type "an object with any subset of the members of interface X". Use case 1: options merging interface Opti...
Open Graph Description: I have two use cases that I cannot seem to type correctly in TypeScript. Both use cases involve functions that take a parameter of type "an object with any subset of the members of interface X". Us...
X Description: I have two use cases that I cannot seem to type correctly in TypeScript. Both use cases involve functions that take a parameter of type "an object with any subset of the members of interface X...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/6218
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sub-type type","articleBody":"I have two use cases that I cannot seem to type correctly in TypeScript. Both use cases involve functions that take a parameter of type \"an object with any subset of the members of interface X\".\n\n**Use case 1: options merging**\n\n```\ninterface Options {\n a: string;\n b: string;\n}\n\nconst defaults: Options = {\n a: \"a\",\n b: \"b\"\n}\n\nfunction merge\u003cT>(base: T, additional: \u003csubtype of T>): T {\n // clone all members of 'additional' onto 'base'\n}\n\nlet opts: Options = merge\u003cOptions>(defaults, { b: \"some other b\" });\n```\n\n**Use case 2: React state**\nSee also https://facebook.github.io/react/docs/component-api.html\n\nA React component is a class with three generic parameters Props, State and Context. The base class defines a setState(newState: State) method. In reality though, the setState() method accepts any sub-type of State.\n\nThis becomes particularly painful when having multiple layers of inheritance in React components:\n\nBaseWidget.tsx\n\n```\n// one React component\nclass BaseWidget\u003cP extends BaseWidget.Props, S extends BaseWidget.State, C extends BaseWidget.Context> extends ReactComponent\u003cP, S, C> {\n\n public componentWillMount()\n // valid React call, TypeScript error 'argument of type foo is not assignable to parameter bar'\n this.setState({ a: \"string\" });\n }\n}\n\nmodule BaseWidget {\n \"use strict\";\n\n export interface Props extends React.Props\u003cBaseWidget\u003cProps, State, Context>> {\n }\n\n // note that all members must have ? on them to work\n export interface State {\n a?: string;\n }\n\n export interface Context {\n }\n}\n\nexport = BaseWidget\n```\n","author":{"url":"https://github.com/rogierschouten","@type":"Person","name":"rogierschouten"},"datePublished":"2015-12-23T10:15:51Z","url":"https://github.com/microsoft/TypeScript/issues/6218"}
| 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:625b9c22-6170-8454-ecbd-f529bfe294a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CF56:3C0CB1:1E12D6:280933:6AFBE12D |
| html-safe-nonce | 558759c6d519e2fdc6bcf28710786963a7cf78d3c39419cfc4c3ff0e0314ba7b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjU2OjNDMENCMToxRTEyRDY6MjgwOTMzOjZBRkJFMTJEIiwidmlzaXRvcl9pZCI6IjMzMTc3NDA4NTM1ODkxNzI1MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ac2af54bb3c4e4adccab5c43203d7440e27855f6d3835071f75963b60f913175 |
| hovercard-subject-tag | issue:123633967 |
| 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/6218/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d2baf8cd01564092e2c5c10ead375597e433756bc47ea3de8301b8f68640704c/microsoft/TypeScript/issues/6218 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d2baf8cd01564092e2c5c10ead375597e433756bc47ea3de8301b8f68640704c/microsoft/TypeScript/issues/6218 |
| og:image:alt | I have two use cases that I cannot seem to type correctly in TypeScript. Both use cases involve functions that take a parameter of type "an object with any subset of the members of interface X". Us... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rogierschouten |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| 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 | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width