Title: Allow conditionally setting optional properties in a mapped type · Issue #36126 · microsoft/TypeScript · GitHub
Open Graph Title: Allow conditionally setting optional properties in a mapped type · Issue #36126 · microsoft/TypeScript
X Title: Allow conditionally setting optional properties in a mapped type · Issue #36126 · microsoft/TypeScript
Description: Search Terms conditional optional mapped type Suggestion Allow us to conditionally apply ? to a property in a mapped type Use Cases I have a function: function myFunction(one: string, two: number, three: number){} but I want to map the p...
Open Graph Description: Search Terms conditional optional mapped type Suggestion Allow us to conditionally apply ? to a property in a mapped type Use Cases I have a function: function myFunction(one: string, two: number, ...
X Description: Search Terms conditional optional mapped type Suggestion Allow us to conditionally apply ? to a property in a mapped type Use Cases I have a function: function myFunction(one: string, two: number, ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36126
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow conditionally setting optional properties in a mapped type","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nconditional optional mapped type \r\n\r\n## Suggestion\r\n\r\nAllow us to conditionally apply `?` to a property in a mapped type\r\n\r\n## Use Cases\r\n\r\nI have a function:\r\n\r\n```\r\nfunction myFunction(one: string, two: number, three: number){}\r\n```\r\n\r\nbut I want to map the params to\r\n\r\n```\r\nfunction myFunction(one: string, two?: number, three?: number){}\r\n```\r\n\r\n## Examples\r\nSuggested approach:\r\n```\r\ntype MapParams\u003cT> = {\r\n [P in keyof T]: T[P] extends string ? T[P] : | ?T[P]\r\n}\r\n```\r\n(or something similar)\r\n\r\nThis is the closest I could come:\r\n\r\n```\r\ntype MapParams\u003cT> = {\r\n [P in keyof T]: T[P] extends string ? T[P] : | T[P] | undefined\r\n}\r\n```\r\n\r\nbut that produces\r\n\r\n```\r\nfunction myFunction(one: string, two: number | undefined, three: number | undefined){}\r\n\r\nmyFunction(\"\", undefined, undefined)\r\n```\r\n\r\n[Playground](http://www.typescriptlang.org/play/?ssl=10&ssc=41&pln=10&pc=1#code/FAMwrgdgxgLglgewgAgLYE8BilaIgCiQFMAuZAZxgCc4IBzAGmRgHcEyIxUAjIqpmAAsqRUsk48+ASmQBvZAF9gwGOgAORZAFkAhmoAKOqjtTkAPABUAfMgC8c4MifIA2vuS1kAayLoEIZAsAXTILNyDkIgAPGCIIABNyCmpaOmQAfkDw5DIAHyz9CPzIeKIQWiJ44CVQHHgkND0NeOxoeoIAOi6jOnIyXQMjE3NDY1QiWKpzVQ1-NCw6vCsrGXka1CbK1twkfAAiPaYSsor4o4STiEqpZQ21Zu32-b2ZAHpXikEEMAAbeOQ2FQvEA)\r\n\r\n## Checklist\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, 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","author":{"url":"https://github.com/Roaders","@type":"Person","name":"Roaders"},"datePublished":"2020-01-10T08:37:36Z","url":"https://github.com/microsoft/TypeScript/issues/36126"}
| 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:8a94ea39-7da6-1b01-8086-e0d86abf60c4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD02:95C88:CA2490:10CCB81:6B00F614 |
| html-safe-nonce | 25abe397f70721526081c74abb50a49410aaa7412bcd5cf0bb7de18e0c3fdcf6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDAyOjk1Qzg4OkNBMjQ5MDoxMENDQjgxOjZCMDBGNjE0IiwidmlzaXRvcl9pZCI6IjkyMTMxNjMzMDk0MDkzMDIwMzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3f2783214bb5ebe59d3dfa4c5bb5e2f3354a4fb2ec15b8d6493202f16808a8f0 |
| hovercard-subject-tag | issue:547939637 |
| 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/36126/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4aa13e28a281bdfc1bb679d240cfb5dc34c31f0a3aaad2c8f6607772bf8a0121/microsoft/TypeScript/issues/36126 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4aa13e28a281bdfc1bb679d240cfb5dc34c31f0a3aaad2c8f6607772bf8a0121/microsoft/TypeScript/issues/36126 |
| og:image:alt | Search Terms conditional optional mapped type Suggestion Allow us to conditionally apply ? to a property in a mapped type Use Cases I have a function: function myFunction(one: string, two: number, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Roaders |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width