René's URL Explorer Experiment


Title: infer Self · Issue #53690 · microsoft/TypeScript · GitHub

Open Graph Title: infer Self · Issue #53690 · microsoft/TypeScript

X Title: infer Self · Issue #53690 · microsoft/TypeScript

Description: Suggestion 🔍 Search Terms Automatic generic parameter Generic type self reference Generic Self keyword Infer Self Exclude from any ✅ Viability Checklist My suggestion meets these guidelines: [x] (I think) This wouldn't be a breaking chan...

Open Graph Description: Suggestion 🔍 Search Terms Automatic generic parameter Generic type self reference Generic Self keyword Infer Self Exclude from any ✅ Viability Checklist My suggestion meets these guidelines: [x] (I...

X Description: Suggestion 🔍 Search Terms Automatic generic parameter Generic type self reference Generic Self keyword Infer Self Exclude from any ✅ Viability Checklist My suggestion meets these guidelines: [x] (I...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/53690

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"infer Self","articleBody":"# Suggestion\r\n\r\n\r\n\r\n## 🔍 Search Terms\r\nAutomatic generic parameter\r\nGeneric type self reference\r\nGeneric Self keyword\r\nInfer Self\r\nExclude from any\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n[x] (I think) This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n[x] (if done right) 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## ⭐ Suggestion\r\nInstead of writing \r\n```typescript\r\ntype DoesTheThing\u003cT> = T extends Something\u003cT> ? T : never\r\n```\r\nand then \r\n```typescript\r\nconst something = aLongVariableName as DoesTheThing\u003ctypeof aLongVariableName>\r\n```\r\nit would be nice to have a way to infer the generic type in these instances.\r\n\r\nLike this: \r\n```typescript\r\ntype DoesTheThing = infer Self extends Something\u003cSelf> ? Self : never\r\n```\r\nwhich could then be used like this: \r\n```typescript\r\nconst something: DoesTheThing = aLongVariableName\r\n```\r\n\r\nWith the risk of being off-topic another thing that would be nice, albeit not as nice as the above, is the ability to Exclude from any.\r\n\r\n```typescript\r\ntype NotAString = Exclude\u003cany, string> // anything that is not a string\r\n```\r\n\r\nI have three variations of my main suggestion, infer Self, that carry varying risks:\r\n1. (least risk) what I've already mentioned. `infer Self` has to be in the beginning of the type definition.\r\n2. (low or no risk) You don't have to write Self but can write any name as long as it's in the start of the expression.\r\n3. (most risky) You have to write Self (or another keyword) but you can use the keyword in other places as well.\r\n\r\n## 📃 Motivating Example\r\n\r\n\r\n```typescript\r\nconst aString = \"some string\"\r\nconst aFunction = (test: string) => \"string\"\r\n\r\n// This is how we have to do it now\r\nexport type NotFunction\u003cT> = Exclude\u003cT, (...args: any) => any>\r\n\r\nconst old1 = aString as NotFunction\u003ctypeof aString>\r\n//    ^\"some string\"\r\nconst old2 = aFunction as NotFunction\u003ctypeof aFunction>\r\n//    ^never\r\n\r\n// Least risky change\r\nexport type NiceNotFunction = infer Self extends Exclude\u003cSelf, (...args: any) => any> ? Self : never\r\n\r\n// Maybe more risky (?) \r\nexport type AlsoNiceNotFunction = infer ThisIsAlsoSelf extends Exclude\u003cThisIsAlsoSelf, (...args: any) => any> ? Self : never\r\n\r\nconst new1: NiceNotFunction = aString\r\n// ^ Should extend string but is any currently\r\n\r\nconst new2: NiceNotFunction = aFunction\r\n// ^ Should be never but is any currently\r\n\r\n\r\n// Most risky/difficult change.\r\n// InferSelf would be overrideable so that it doesn't mess up old codebases.\r\n// It could also be a different keyword that would be a syntax error before this change, \r\n// for example by including a space.\r\nexport type NicerNotFunction = Exclude\u003cInferSelf, (...args: any) => any>\r\n\r\nconst new3: NicerNotFunction = aString\r\n// ^ Should extend string but is any currently\r\n\r\nconst new4: NicerNotFunction = aFunction\r\n// ^ Should be never but is any currently\r\n```\r\n\r\n\r\n\r\n## 💻 Use Cases\r\n\r\nGoogle `jsonable typescript` to get lots of examples, the top result being [this](https://dev.to/ankittanna/how-to-create-a-type-for-complex-json-object-in-typescript-d81). There's also been another typescript [suggestion](https://github.com/microsoft/TypeScript/issues/1897) to add JSON as a default type.\r\n\r\nIn the article, the author suggests doing this:\r\n\r\n```typescript\r\ntype JSONValue =\r\n    | string\r\n    | number\r\n    | boolean\r\n    | JSONObject\r\n    | JSONArray;\r\n\r\ninterface JSONObject {\r\n    [x: string]: JSONValue;\r\n}\r\n\r\ninterface JSONArray extends Array\u003cJSONValue> { }\r\n```\r\n\r\nThis works but it's a bit verbose for what I wanted. I also wanted to allow undefined and null, and in that case basically anything can be JSON serialized except for functions and symbols if I'm not mistaken. \r\n\r\nSo I went on to google on how to make a NotFunction type. [This](https://stackoverflow.com/questions/24613955/is-there-a-type-in-typescript-for-anything-except-functions) was the top result, and they suggest doing this:\r\n\r\n```typescript\r\ntype NotFunction\u003cT> = T extends Function ? never : T;\r\n```\r\n\r\nWhich brings me to what I've already said.","author":{"url":"https://github.com/henrikeliq","@type":"Person","name":"henrikeliq"},"datePublished":"2023-04-06T16:26:29Z","url":"https://github.com/microsoft/TypeScript/issues/53690"}

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:16b01f3e-7b1a-76db-a3f2-3caec826d6a9
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB22A:21AC3F:113B205:15F2579:6B01027B
html-safe-nonce5b4d0f0a7d5e662a34e8a258e3c755c94efc388d3ae59940e611e826dfdfebe0
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjJBOjIxQUMzRjoxMTNCMjA1OjE1RjI1Nzk6NkIwMTAyN0IiLCJ2aXNpdG9yX2lkIjoiMjA3OTM5NjgzNzg5ODk4MzYzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac4497ccf0bfa8f473fbb2bc21197f06253e6d9018ae277a3b7f6b88ef4a16a13a
hovercard-subject-tagissue:1657720312
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/53690/issue_layout
twitter:imagehttps://opengraph.githubassets.com/b6aadbbf5f1fc21d4849a745e1e5dd81f5a05c767b1f52f78bd940058d578061/microsoft/TypeScript/issues/53690
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/b6aadbbf5f1fc21d4849a745e1e5dd81f5a05c767b1f52f78bd940058d578061/microsoft/TypeScript/issues/53690
og:image:altSuggestion 🔍 Search Terms Automatic generic parameter Generic type self reference Generic Self keyword Infer Self Exclude from any ✅ Viability Checklist My suggestion meets these guidelines: [x] (I...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamehenrikeliq
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
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
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/53690#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F53690
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%2F53690
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/53690
Reloadhttps://github.com/microsoft/TypeScript/issues/53690
Reloadhttps://github.com/microsoft/TypeScript/issues/53690
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests309 (309)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/53690
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/53690
infer Selfhttps://github.com/microsoft/TypeScript/issues/53690#top
https://github.com/henrikeliq
henrikeliqhttps://github.com/henrikeliq
on Apr 6, 2023https://github.com/microsoft/TypeScript/issues/53690#issue-1657720312
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
thishttps://dev.to/ankittanna/how-to-create-a-type-for-complex-json-object-in-typescript-d81
suggestionhttps://github.com/microsoft/TypeScript/issues/1897
Thishttps://stackoverflow.com/questions/24613955/is-there-a-type-in-typescript-for-anything-except-functions
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Apr 6, 2023https://github.com/microsoft/TypeScript/issues/53690#issuecomment-1499459127
https://github.com/henrikeliq
henrikeliqhttps://github.com/henrikeliq
on Apr 6, 2023https://github.com/microsoft/TypeScript/issues/53690#issuecomment-1499460903
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Apr 6, 2023https://github.com/microsoft/TypeScript/issues/53690#issuecomment-1499480096
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on Apr 7, 2023https://github.com/microsoft/TypeScript/issues/53690#issuecomment-1500082051
herehttps://github.com/microsoft/TypeScript/pull/52088
https://github.com/henrikeliq
henrikeliqhttps://github.com/henrikeliq
on Apr 8, 2023https://github.com/microsoft/TypeScript/issues/53690#issuecomment-1500940675
Mateusz Burzyński (@Andarist)https://github.com/Andarist
henrikeliqhttps://github.com/henrikeliq
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 8, 2023https://github.com/microsoft/TypeScript/issues/53690#event-8957975782
henrikeliqhttps://github.com/henrikeliq
on Apr 8, 2023https://github.com/microsoft/TypeScript/issues/53690#event-8957976009
henrikeliqhttps://github.com/henrikeliq
not plannedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Anot-planned
on Apr 8, 2023https://github.com/microsoft/TypeScript/issues/53690#event-8957976212
microsofthttps://github.com/microsoft
on Oct 22, 2025https://github.com/microsoft/TypeScript/issues/53690#event-20419535742
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/53690
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/53690
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.