Title: Improve type readability (fix type alias propagation, allow aliasing inferred types) · Issue #45954 · microsoft/TypeScript · GitHub
Open Graph Title: Improve type readability (fix type alias propagation, allow aliasing inferred types) · Issue #45954 · microsoft/TypeScript
X Title: Improve type readability (fix type alias propagation, allow aliasing inferred types) · Issue #45954 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms preserve type aliases,inferred types,type names,readability,type expansion ✅ Viability Checklist My suggestion meets these guidelines: [*] This wouldn't be a breaking change in existing TypeScript/JavaScript cod...
Open Graph Description: Suggestion 🔍 Search Terms preserve type aliases,inferred types,type names,readability,type expansion ✅ Viability Checklist My suggestion meets these guidelines: [*] This wouldn't be a breaking chan...
X Description: Suggestion 🔍 Search Terms preserve type aliases,inferred types,type names,readability,type expansion ✅ Viability Checklist My suggestion meets these guidelines: [*] This wouldn't be a breaking ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/45954
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve type readability (fix type alias propagation, allow aliasing inferred types)","articleBody":"# Suggestion\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\npreserve type aliases,inferred types,type names,readability,type expansion\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [*] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [*] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [*] This could be implemented without emitting different JS based on the types of the expressions\r\n* [*] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [*] 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\r\n\r\nOne thing that makes working with Typescript's type system occasionally hopeless is when cumulative inferred types are displayed in type expansion and 'pollute' the information in quick info or errors.\r\n\r\nSo the proposal is two fold:\r\n\r\n1. Fix type expansion so that the names of type aliases are preferred over their expanded inferred types wherever a type alias is available.\r\n2. Add syntax that allows us to 'alias'/name inferred types and allow those alias names to carry through type expansion.\r\n\r\n## 📃 Motivating Example\r\n\r\nWith any kind of sophisticated typing, we quickly end up with error messages and parameter info that takes multiple seconds to load, and shows a truncated mess of type syntax. Worse, the expansion of these inferred types accumulates and adversely affects any code that touches them.\r\n\r\nTypescript fails us in two distinct areas with regard to type expansion, and the culprit seems to be inferred types...\r\n\r\n### 1. Typescript doesn't always respect type aliases, even when they are explicitly provided\r\n\r\nE.g. First, what works:\r\n\r\n```typescript\r\n// Let's start with a contrived mapped type:\r\n\r\ntype Mapped\u003cT> = {\r\n [P in keyof T]: never;\r\n};\r\n\r\n// And a generic function that takes an argument and returns the mapped type of it.\r\n\r\nfunction map\u003cT>(value: T): Mapped\u003cT> {\r\n return {} as never;\r\n}\r\n\r\n// Create a Person via inference\r\nconst p = {age: 5, name: 'Bobby'};\r\n\r\n// And quick info on 'inferred' displays: Mapped\u003c{age: number, name: string}>\r\nconst inferred = map(p);\r\n\r\n// Let's clean up the inferred type by defining a type alias.\r\ntype Person = {age: number; name: string};\r\n\r\n// Now quick info on 'named' shows: Mapped\u003cPerson>\r\nconst named = map({age: 5, name: 'Bobby'} as Person);\r\n\r\n```\r\n\r\nSo far, so good. The rule seems to be that if we explicitly alias a type, Typescript will use that name and terminate expansion.\r\n\r\nBut this rule gets broken when there is logic to the type alias.\r\n\r\n```typescript\r\n// Use `io-ts` as an example and declare runtime types/validators like so:\r\n\r\nimport * as t from 'io-ts';\r\n\r\nconst Person = t.type({\r\n name: t.string,\r\n age: t.number,\r\n});\r\n\r\n// And infer the underlying data type from it...\r\ntype Person = t.TypeOf\u003ctypeof Person>;\r\n\r\n// Now, let's put them through the same Mapped type and map function...\r\n\r\n// We use the type alias 'Person' in the same manner to try to 'name' the type, but...\r\nconst named = map({age: 5, name: 'Bobby'} as Person);\r\n\r\n// Quick info on 'named' is: Mapped\u003c{name: string, age: number}> not Mapped\u003cPerson>\r\n```\r\n\r\nYou could argue that this is handy, but the consequences of this deviation from the rule means that all other types referencing this one will repeat the full expansion.\r\n\r\n### 2. There is no way to name an inferred type\r\n\r\nThere is also a scenario where fully expanded inferred types are unavoidable. More specifically, where there is no way to explicitly specify a type alias whose name should be displayed. E.g.\r\n\r\n```typescript\r\n// Let's return to our Person type and try some composition:\r\n\r\nconst Family = t.type({\r\n mother: Person,\r\n father: Person,\r\n child: Person,\r\n});\r\n\r\n// The type of 'Family' is: t.TypeC\u003c{mother: t.TypeC\u003c{name: t.StringC, age: t.NumberC}>, father: t.TypeC\u003c{name: t.StringC, age: t.NumberC}>, child: t.TypeC\u003c{name: t.StringC, age: t.NumberC}>}>\r\n\r\n```\r\n\r\nThat's already pretty out of hand. All we would need to know, ideally, is something like (pseudocode) `t.TypeC\u003c{mother: Person, father: Person, child: Person}>`.\r\n\r\nBut since the type of `Person` is inferred from the function's return type, we don't get a chance to create a type alias for it. In fact, that's the reason we're able to declare a type alias of the same name for the underlying data type.\r\n\r\nSo wherever we use the Person instance, we're now stuck with a fully expanded inferred type. If we were able to provide Typescript with an alias for the inferred type, we could escape expansion and clean up type expansions everywhere.\r\n\r\n### Proposed `as type` syntax\r\n\r\nSuch syntax could work like this:\r\n```typescript\r\n// Combining the syntax for type assertion and type alias declaration:\r\nconst Person = t.type({\r\n name: t.string,\r\n age: t.number,\r\n}) as type PersonType;\r\n```\r\nSo that Family would then display: `t.TypeC\u003c{mother: PersonType, father: PersonType, child: PersonType}>`\r\n\r\nSince the above kind of factory that returns an inferred type is a common pattern, it could also be shorthanded like this:\r\n\r\n```typescript\r\nconst type PersonType = t.type({\r\n name: t.string,\r\n age: t.number,\r\n});\r\n```\r\n\r\n...so that the name of the variable would double as the name of the type alias.\r\n\r\nThere could even be syntax that is used upstream by the function returning the inferred type...\r\n```typescript\r\nfunction map\u003cT>(value: T): Mapped\u003cT> as type new {\r\n return {} as never;\r\n}\r\n\r\n// Here, the type of `MyMap` is `MyMap` (it gets aliased by default with the same name).\r\nconst MyMap = map({ age: 22, name: 'Bobby});\r\n```\r\n\r\nThe MyMap alias expanded is Mapped\u003c{ age: number, name: string }>\r\n\r\n```typescript\r\n// We could also allow for when wrapping is needed...\r\nfunction get\u003cT>(value: T): Wrapper\u003cT as type new> {\r\n return {} as never;\r\n}\r\n\r\n// Here Person instance is typed as Wrapped\u003cPerson>, where Person is { age: number, name: string }\r\nconst Person = get({ age: 22, name: 'Bobby' })\r\n```\r\n\r\n## 💻 Use Cases\r\n\r\nI think I've covered quite specific use cases in my motivating example. \r\n\r\nLeveraging type inference to construct sophisticated types is common enough now that we should pay particular attention to developer experience for type systems. \r\n\r\nThese two changes would especially allow both developers and library authors to have better control over the types their code emits to intellisense and allow them to craft an improved developer experience.\r\n\r\nSpecifically, we could use libraries like `io-ts` and `zod` without polluting our type intellisense.","author":{"url":"https://github.com/martaver","@type":"Person","name":"martaver"},"datePublished":"2021-09-18T20:06:04Z","url":"https://github.com/microsoft/TypeScript/issues/45954"}
| 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:13e65a61-7c0b-4344-d655-5aa963f1e744 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E074:361639:266847A:334D8CB:6B01ACEF |
| html-safe-nonce | 65b0cb40048b413bb8160660b438caffcb11304c822f13c7ec101dc384f7a719 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDc0OjM2MTYzOToyNjY4NDdBOjMzNEQ4Q0I6NkIwMUFDRUYiLCJ2aXNpdG9yX2lkIjoiODA4NjQ5NTcyNDQwNzIwNTEwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ff0e66dd0739eb59b179a9143ba530cd4488b692f0be6ffbc8bac323c71236a9 |
| hovercard-subject-tag | issue:1000122360 |
| 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/45954/issue_layout |
| twitter:image | https://opengraph.githubassets.com/63e4defdb4b0c6e164de3e36454bee43f2ee8b05be58319306686601ea1d922f/microsoft/TypeScript/issues/45954 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/63e4defdb4b0c6e164de3e36454bee43f2ee8b05be58319306686601ea1d922f/microsoft/TypeScript/issues/45954 |
| og:image:alt | Suggestion 🔍 Search Terms preserve type aliases,inferred types,type names,readability,type expansion ✅ Viability Checklist My suggestion meets these guidelines: [*] This wouldn't be a breaking chan... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | martaver |
| 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