Title: For a given type, derive a new type where all the properties specified in the original type are optional · Issue #7355 · microsoft/TypeScript · GitHub
Open Graph Title: For a given type, derive a new type where all the properties specified in the original type are optional · Issue #7355 · microsoft/TypeScript
X Title: For a given type, derive a new type where all the properties specified in the original type are optional · Issue #7355 · microsoft/TypeScript
Description: Hey Typescript, React's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the documentation (at https://facebook.github.io/...
Open Graph Description: Hey Typescript, React's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the docum...
X Description: Hey Typescript, React's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the d...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7355
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"For a given type, derive a new type where all the properties specified in the original type are optional","articleBody":"Hey Typescript,\n\nReact's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the documentation (at https://facebook.github.io/react/docs/component-api.html#setstate) specifies only that:\n\n> The first argument can be an object (containing zero or more keys to update)...\n\nThat is, callers aren't required to specify _all_ properties on the state, but only a subset of properties that they're updating. Unfortunately, there's currently no way to express this in the type system. People workaround this by defining type S as having all optional properties to begin with, even if some properties should be required.\n\nIt'd be great if Typescript had a some type operator that, when applied to an existing type, could construct a new type where all the properties specified in the original type are optional.\n\nExample of use:\n\n``` typescript\nclass Component\u003cS> {\n setState(state: S?): void;\n // example syntax - \"S?\" is the type S but where all properties are optional\n}\n\ninterface MyState {\n foo: string;\n bar: string;\n}\n\nclass MyComponent extends Component\u003cMyState> {\n constructor() {\n this.state = {foo: \"a\", bar: \"b\"};\n }\n\n function updateFoo() {\n // this should pass\n this.setState({ foo: \"newFoo\" });\n\n // you can also specify that a value is of an optional type\n const updates: MyState? = {\n bar: \"newBar\"\n };\n this.setState(updates);\n\n // of course, properties not in the original type are still not allowed\n // this should throw \"'nonExistentProperty' does not exist in type 'MyState?'\"\n const bad: MyState? = {\n nonExistentProperty: 123\n };\n }\n\n}\n```\n\nLet me know if there's anything else I could clarify, thanks for your time!\n","author":{"url":"https://github.com/hellochar","@type":"Person","name":"hellochar"},"datePublished":"2016-03-03T01:08:51Z","url":"https://github.com/microsoft/TypeScript/issues/7355"}
| 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:7010b147-f5da-726c-8805-df94ff03b347 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 979A:683FF:4A9242:6210C5:6B00C9AB |
| html-safe-nonce | 07faf100815e579d5eee8a1d0d58d2642fad56aef338c27fdb24102381418196 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzlBOjY4M0ZGOjRBOTI0Mjo2MjEwQzU6NkIwMEM5QUIiLCJ2aXNpdG9yX2lkIjoiNzEwODc5ODczMTE1MTQ2NjY3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 259714633e0da04040a869b8627ae794b1af59b8671c294bf030d056b30530cb |
| hovercard-subject-tag | issue:138043360 |
| 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/7355/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea1c800c532d70099cf1bb23bda8b0a5a22229d6f354eb2f321939fcdee4003e/microsoft/TypeScript/issues/7355 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea1c800c532d70099cf1bb23bda8b0a5a22229d6f354eb2f321939fcdee4003e/microsoft/TypeScript/issues/7355 |
| og:image:alt | Hey Typescript, React's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the docum... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hellochar |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width