Title: More accurate typing of Object.assign and React component setState() · Issue #6613 · microsoft/TypeScript · GitHub
Open Graph Title: More accurate typing of Object.assign and React component setState() · Issue #6613 · microsoft/TypeScript
X Title: More accurate typing of Object.assign and React component setState() · Issue #6613 · microsoft/TypeScript
Description: This is a proposal for solving the second use case mentioned in Issue #6218 Problem /** * This class is a given by the React framework */ class ReactComponent { protected _state: S; /** * This method actually accepts any supertype of ...
Open Graph Description: This is a proposal for solving the second use case mentioned in Issue #6218 Problem /** * This class is a given by the React framework */ class ReactComponent { protected _state: S; /** * This m...
X Description: This is a proposal for solving the second use case mentioned in Issue #6218 Problem /** * This class is a given by the React framework */ class ReactComponent<S> { protected _state: S; /** * ...
Mail addresses
notifications@github.com
Opengraph URL: https://github.com/microsoft/TypeScript/issues/6613
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"More accurate typing of Object.assign and React component setState()","articleBody":"This is a proposal for solving the second use case mentioned in Issue #6218\n## Problem\n\n``` javascript\n\n/**\n * This class is a given by the React framework\n */\nclass ReactComponent\u003cS> {\n protected _state: S;\n\n /**\n * This method actually accepts any supertype of S\n */\n protected setState(newState: S): void {\n for (let name in newState) {\n if (newState.hasOwnProperty(name)) {\n this._state[name] = newState[name];\n }\n }\n }\n\n protected componentWillMount(): void {\n // abstract\n }\n}\n\n/**\n * Some state interface declaration. Note all members are optional to allow setState to\n * be called with supertypes of BaseState\n */\ninterface BaseState {\n a?: string;\n}\n\n/**\n * My own base class for certain React widgets\n */\nclass BaseWidget\u003cS extends BaseState> extends ReactComponent\u003cS> {\n\n constructor() { \n super();\n this._state = {};\n }\n\n protected componentWillMount(): void {\n this.setState({ a: \"boo\" });\n }\n} \n\n```\n\n```\n$ tsc v1.ts\nv1.ts(39,9): error TS2322: Type '{}' is not assignable to type 'S'.\nv1.ts(43,23): error TS2345: Argument of type '{ a: string; }' is not assignable to parameter of type 'S'.\n```\n\nThe compiler cannot know that the setState() method will accept any super-type of S, i.e. an object with any subset of the members of S.\n## Proposal\n\nAdd a keyword to type setState() properly. To avoid confusion, I chose **partof** instead of e.g. 'supertype of' (see also initial confusion in #6218). For me, 'partof' conveys that I can give the method any object with a subset of the members of S. \n\n``` javascript\n/**\n * This class is a given by the React framework\n */\nclass ReactComponent\u003cS extends {}> {\n protected _state: S;\n\n /**\n * This method accepts any supertype of S\n */\n protected setState(newState: partof S): void {\n }\n}\n```\n\n**Properties of partof**\n- Only works for object types (hence the 'extends {}' above) because I wouldn't know how to pass part of e.g. a string\n- Allows any supertype of the given type\n- Incorporates knowledge of the generic parameter. Given the class declaration `class ReactComponent\u003cS extends { foo: number; }>`, one is able to call `setState({ foo: 3})` and `setState({})` but not `setState({ bar: 3 })` inside the class definition.\n\nComments more than welcome, I'm not a compiler expert. This is just to get the discussion going. If you think there already exists a way of typing this, please check with the original issue for a couple of failed examples.\n","author":{"url":"https://github.com/rogierschouten","@type":"Person","name":"rogierschouten"},"datePublished":"2016-01-25T19:36:53Z","url":"https://github.com/microsoft/TypeScript/issues/6613"}
| 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:8395668b-e351-2b4c-3ca7-3d69a9d2b0d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCE0:E5741:15F342:1D30E5:6AFBDE26 |
| html-safe-nonce | 013d57f012193a4d33aa8b684983aa745149e0a3788b10a241322e201217a8ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0UwOkU1NzQxOjE1RjM0MjoxRDMwRTU6NkFGQkRFMjYiLCJ2aXNpdG9yX2lkIjoiNjgxNDQwOTgyODE4NjQ0NzM5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0b180b737889152e751cc1a3472b4773ebff30a3dfae599f031c36366d178d4d |
| hovercard-subject-tag | issue:128618856 |
| 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/6613/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2ceca45154a1805ee9a22d3dfc8a706e0285c58132252eeff3317be5a026a0e3/microsoft/TypeScript/issues/6613 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2ceca45154a1805ee9a22d3dfc8a706e0285c58132252eeff3317be5a026a0e3/microsoft/TypeScript/issues/6613 |
| og:image:alt | This is a proposal for solving the second use case mentioned in Issue #6218 Problem /** * This class is a given by the React framework */ class ReactComponent |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width