René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:13e65a61-7c0b-4344-d655-5aa963f1e744
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE074:361639:266847A:334D8CB:6B01ACEF
html-safe-nonce65b0cb40048b413bb8160660b438caffcb11304c822f13c7ec101dc384f7a719
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDc0OjM2MTYzOToyNjY4NDdBOjMzNEQ4Q0I6NkIwMUFDRUYiLCJ2aXNpdG9yX2lkIjoiODA4NjQ5NTcyNDQwNzIwNTEwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacff0e66dd0739eb59b179a9143ba530cd4488b692f0be6ffbc8bac323c71236a9
hovercard-subject-tagissue:1000122360
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/45954/issue_layout
twitter:imagehttps://opengraph.githubassets.com/63e4defdb4b0c6e164de3e36454bee43f2ee8b05be58319306686601ea1d922f/microsoft/TypeScript/issues/45954
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/63e4defdb4b0c6e164de3e36454bee43f2ee8b05be58319306686601ea1d922f/microsoft/TypeScript/issues/45954
og:image:altSuggestion 🔍 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemartaver
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/45954#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F45954
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F45954
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/45954
Reloadhttps://github.com/microsoft/TypeScript/issues/45954
Reloadhttps://github.com/microsoft/TypeScript/issues/45954
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/45954
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/45954
Improve type readability (fix type alias propagation, allow aliasing inferred types)https://github.com/microsoft/TypeScript/issues/45954#top
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/martaver
Sebastian Nemeth (martaver)https://github.com/martaver
on Sep 18, 2021https://github.com/microsoft/TypeScript/issues/45954#issue-1000122360
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Andrew Branch (andrewbranch)https://github.com/andrewbranch
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Sep 23, 2021https://github.com/microsoft/TypeScript/issues/45954#event-5352171932
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on Apr 21, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1104982336
https://github.com/Galbar
Alessio Linares (Galbar)https://github.com/Galbar
on Jun 9, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1150817159
https://github.com/essential-randomness
Ms. Boba (essential-randomness)https://github.com/essential-randomness
on Nov 28, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1328417858
https://github.com/martaver
Sebastian Nemeth (martaver)https://github.com/martaver
on Nov 28, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1328695778
https://github.com/geoffreytools
Geoffrey (geoffreytools)https://github.com/geoffreytools
on Nov 28, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1329216315
https://github.com/martaver
Sebastian Nemeth (martaver)https://github.com/martaver
on Nov 28, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1329334418
https://github.com/essential-randomness
Ms. Boba (essential-randomness)https://github.com/essential-randomness
on Nov 28, 2022https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1329624733
https://github.com/ddurschlag
ddurschlaghttps://github.com/ddurschlag
on Oct 12, 2023https://github.com/microsoft/TypeScript/issues/45954#issuecomment-1758739192
#42149https://github.com/microsoft/TypeScript/pull/42149
#42284https://github.com/microsoft/TypeScript/pull/42284
Sebastian Nemeth (@martaver)https://github.com/martaver
Geoffrey (@geoffreytools)https://github.com/geoffreytools
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/45954
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/45954
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.