Title: Conditionally optional/conditionally readonly properties · Issue #44261 · microsoft/TypeScript · GitHub
Open Graph Title: Conditionally optional/conditionally readonly properties · Issue #44261 · microsoft/TypeScript
X Title: Conditionally optional/conditionally readonly properties · Issue #44261 · microsoft/TypeScript
Description: Conditionally-Optional Properties (in object types and interfaces) 🔍 Search Terms conditionally optional conditional optional conditionally readonly conditional readonly These issues are related, but not exactly the same: #36126 #31409 ✅...
Open Graph Description: Conditionally-Optional Properties (in object types and interfaces) 🔍 Search Terms conditionally optional conditional optional conditionally readonly conditional readonly These issues are related, b...
X Description: Conditionally-Optional Properties (in object types and interfaces) 🔍 Search Terms conditionally optional conditional optional conditionally readonly conditional readonly These issues are related, b...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44261
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conditionally optional/conditionally readonly properties","articleBody":"# Conditionally-Optional Properties (in object types and interfaces)\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\n- conditionally optional\r\n- conditional optional\r\n- conditionally readonly\r\n- conditional readonly\r\n\r\n\r\nThese issues are related, but not exactly the same:\r\n\r\n- #36126\r\n- #31409\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion / 📃 Motivating Example\r\n\r\nA new syntax and associated type-checking making it possible to mark a property as \"conditionally-optional\", for example:\r\n\r\n```ts\r\ninterface Calculate\u003cT> {\r\n items: T[];\r\n \r\n /**\r\n * Converts an item into a string for comparison.\r\n * If `T` is already a string type, this is optional.\r\n */ \r\n getKey(? if T extends string): (item: T) => string;\r\n}\r\n```\r\n\r\nthe intent here is that `getKey` is optional if `T` is `string` or a subtype of `string`, but otherwise `getKey` is mandatory. Thus, an implementation could (for example) provide a default implementation `getKey = str => str`.\r\n\r\nIt's currently possible to write types that accomplish this using a mixture of intersections and conditionally-mapped types. However, you lose certain important ergonomic attributes:\r\n\r\n- the syntax for intersection types, mapped types, and conditional types is much more complicated and much less familiar than interface/object types\r\n- conditional types and complex mappings are likely to lose jsdoc comments, defeating intellisense when developers are later trying to use the type\r\n- complex types can't be `extend`ed whereas interfaces and object types can be\r\n\r\n## 💻 Use Cases\r\n\r\nIn general, any attribute of a property could be made conditional in the same way:\r\n\r\n```ts\r\ntype Example = \u003cT, Flag extends boolean, Active extends boolean> = {\r\n // conditionally optional:\r\n convertToString (? if T extends string): (item: T) => string;\r\n\r\n // optional; made conditionally required:\r\n initialValue? (-? if T extends boolean): T; \r\n\r\n // conditionally readonly:\r\n (readonly if Flag extends \"permanent\") flagValue: boolean;\r\n\r\n // conditionally defined:\r\n (parentId if Active extends true): string;\r\n}\r\n```\r\n\r\nIn most cases, a basic (but incomplete) workaround exists: use the less-restrictive form everywhere. For example, if it's going to mostly be _used_ and you don't want to forget to pass a property, just make it required; if it's mostly going to be accessed and you don't want to forget that a property is present, just make it optional, etc.\r\n\r\nAllowing these values to be set conditionally would just make it easier to express certain more-complex domain-specific constraints, while keeping the types mostly self-contained and readable.","author":{"url":"https://github.com/Nathan-Fenner","@type":"Person","name":"Nathan-Fenner"},"datePublished":"2021-05-26T00:50:18Z","url":"https://github.com/microsoft/TypeScript/issues/44261"}
| 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:427c81d1-0bf7-d581-50e2-36515cb97b51 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 833E:22DB35:99E65B:C8435A:6B00E0EF |
| html-safe-nonce | 1a856e3a54d7a8241a7b70580ca9da2d43c9d6aa7c738dd881f8c0ca266e1bdf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzNFOjIyREIzNTo5OUU2NUI6Qzg0MzVBOjZCMDBFMEVGIiwidmlzaXRvcl9pZCI6IjYwMjQ4OTI1MjAxNzYwODMxODMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d549b20e852b9bf36b51137880d80abd5e75dc1cf9ae5640345fa26e4506161b |
| hovercard-subject-tag | issue:901568992 |
| 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/44261/issue_layout |
| twitter:image | https://opengraph.githubassets.com/53df07b9d5dd7f13c373dd9b4a2e276769b9e5f79cb6e49ff8ca64cd28e7c6e2/microsoft/TypeScript/issues/44261 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/53df07b9d5dd7f13c373dd9b4a2e276769b9e5f79cb6e49ff8ca64cd28e7c6e2/microsoft/TypeScript/issues/44261 |
| og:image:alt | Conditionally-Optional Properties (in object types and interfaces) 🔍 Search Terms conditionally optional conditional optional conditionally readonly conditional readonly These issues are related, b... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Nathan-Fenner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width