Title: [Feature Request] Future-proof always-aliasing/never-expanding of mapped/intersection/union/etc. types · Issue #35654 · microsoft/TypeScript · GitHub
Open Graph Title: [Feature Request] Future-proof always-aliasing/never-expanding of mapped/intersection/union/etc. types · Issue #35654 · microsoft/TypeScript
X Title: [Feature Request] Future-proof always-aliasing/never-expanding of mapped/intersection/union/etc. types · Issue #35654 · microsoft/TypeScript
Description: Search Terms Force TS to always alias type, optional property, never expand type Suggestion It would be nice to have a way to annotate a type alias and tell TS, "As much as possible, do not expand this type alias in emit". I don't know w...
Open Graph Description: Search Terms Force TS to always alias type, optional property, never expand type Suggestion It would be nice to have a way to annotate a type alias and tell TS, "As much as possible, do not expand ...
X Description: Search Terms Force TS to always alias type, optional property, never expand type Suggestion It would be nice to have a way to annotate a type alias and tell TS, "As much as possible, do not ex...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35654
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Feature Request] Future-proof always-aliasing/never-expanding of mapped/intersection/union/etc. types","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\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\n\r\nForce TS to always alias type, optional property, never expand type\r\n\r\n## Suggestion\r\n\r\nIt would be nice to have a way to annotate a type alias and tell TS, \"As much as possible, do not expand this type alias in emit\". I don't know what syntax it should have. But maybe,\r\n```ts\r\ntype interface PleaseDoNotExpand\u003cT> = /*complex type*/;\r\n```\r\n\r\nIn all aspects, the above is a type alias, except for the fact that its type will not expand in emit (as much as possible).\r\n\r\n`class` and `interface` types have this behaviour. Their identifiers are used as much as possible in emit.\r\n\r\nThe \"readability\" of a particular emit is almost always subjective. So, giving developers some control over the emit can make code easier to understand.\r\n\r\n-----\r\n\r\nHere's an example, where using a type alias in a union causes the type alias' identifier to not be used in emit,\r\n```ts\r\ntype PleaseDoNotExpand\u003cT> =\r\n | T\r\n | { x : T }\r\n;\r\n\r\n/*\r\nExpected: const a: PleaseDoNotExpand\u003cnumber>\r\nActual : const a: PleaseDoNotExpand\u003cnumber>\r\n*/\r\ndeclare const a: PleaseDoNotExpand\u003cnumber>;\r\n\r\n\r\n/*\r\nExpected: const b: PleaseDoNotExpand\u003cnumber>|undefined\r\nActual : const b: number | {x: number;} | undefined\r\n*/\r\ndeclare const b: PleaseDoNotExpand\u003cnumber>|undefined;\r\n```\r\n\r\nIf `PleaseDoNotExpand\u003c>` were a `class` or `interface`, we would have `const b: PleaseDoNotExpand\u003cnumber>|undefined`\r\n\r\n## Use Cases\r\n\r\n### What do you want to use this for?\r\n\r\n+ Better emit for `.d.ts` files\r\n+ Better emit for tooltip hover\r\n+ Better emit for error messages\r\n\r\nSome type aliases are hundreds of lines long, after expansion. These type aliases usually have short, intuitive identifiers. But those identifiers tend to get lost when used in union types (and optional properties). See https://github.com/microsoft/TypeScript/issues/35616 for more examples.\r\n\r\n### What shortcomings exist with current approaches?\r\n\r\nThere is no \"general purpose\" workaround for the current problem.\r\n\r\nSo far, I've thought of two workarounds. But they only work for very specific use cases.\r\n\r\n#### Workaround 1: The type alias has statically known members\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/34777\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/34777#issuecomment-551993933\r\n\r\nThe idea is to use an `interface` to extend the type alias. From that point, only the `interface`'s identifier is used in emit. This has the most desirable behaviour. If it could be extended to work for all use cases, then I wouldn't have this feature request.\r\n\r\n#### Workdaround 2: The type alias is being removed by unions/optional properties\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/35616\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/35616#issuecomment-564894944\r\n\r\nThe idea is to create a new type alias that is a union of the original type and the new union elements. However, it does not always work and I don't know why. But this is better than always expanding.\r\n\r\n## Examples\r\n\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n\r\n```ts\r\n//No idea about syntax\r\ntype interface PleaseDoNotExpand\u003cT> =\r\n | T\r\n | { x : T }\r\n;\r\n\r\n/*\r\nconst a: PleaseDoNotExpand\u003cnumber>\r\n*/\r\ndeclare const a: PleaseDoNotExpand\u003cnumber>;\r\n\r\n\r\n/*\r\nconst b: PleaseDoNotExpand\u003cnumber>|undefined\r\n*/\r\ndeclare const b: PleaseDoNotExpand\u003cnumber>|undefined;\r\n\r\n/*\r\n//It makes sense to lose the identifier at this point\r\nconst c: number\r\n*/\r\ndeclare const c: Extract\u003cPleaseDoNotExpand\u003cnumber>, number>;\r\n\r\n/*\r\n//It makes sense to lose the identifier at this point\r\nconst c: { x : number }\r\n*/\r\ndeclare const d: Exclude\u003cPleaseDoNotExpand\u003cnumber>, number>;\r\n```\r\n\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\r\n## Related\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/34556 asks to never alias a type. This asks to always alias a type.","author":{"url":"https://github.com/AnyhowStep","@type":"Person","name":"AnyhowStep"},"datePublished":"2019-12-12T19:43:28Z","url":"https://github.com/microsoft/TypeScript/issues/35654"}
| 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:225950a2-f49d-b506-59cb-c15b755ccf07 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A506:19F84C:2D13435:394FCE6:6B01A2A9 |
| html-safe-nonce | ca2ea23cb92a2d58a9e29e7b63c4d1329e7c4e0d9df66a9a1d7ad73f447736e6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTA2OjE5Rjg0QzoyRDEzNDM1OjM5NEZDRTY6NkIwMUEyQTkiLCJ2aXNpdG9yX2lkIjoiMTYyNDY2ODUyOTM3MjUzNzUxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 232284a03f31fc151ec145fe072d9ce551a23817c6b46634a7c55848cb4287ad |
| hovercard-subject-tag | issue:537176460 |
| 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/35654/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8dfe50e83c88adc378f61a87e10f6d1159c204e5efd71a3ff3e8cd265292079e/microsoft/TypeScript/issues/35654 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8dfe50e83c88adc378f61a87e10f6d1159c204e5efd71a3ff3e8cd265292079e/microsoft/TypeScript/issues/35654 |
| og:image:alt | Search Terms Force TS to always alias type, optional property, never expand type Suggestion It would be nice to have a way to annotate a type alias and tell TS, "As much as possible, do not expand ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AnyhowStep |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width