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
Domain: github.com
{"@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-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:16b01f3e-7b1a-76db-a3f2-3caec826d6a9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B22A:21AC3F:113B205:15F2579:6B01027B |
| html-safe-nonce | 5b4d0f0a7d5e662a34e8a258e3c755c94efc388d3ae59940e611e826dfdfebe0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjJBOjIxQUMzRjoxMTNCMjA1OjE1RjI1Nzk6NkIwMTAyN0IiLCJ2aXNpdG9yX2lkIjoiMjA3OTM5NjgzNzg5ODk4MzYzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4497ccf0bfa8f473fbb2bc21197f06253e6d9018ae277a3b7f6b88ef4a16a13a |
| hovercard-subject-tag | issue:1657720312 |
| 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/53690/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b6aadbbf5f1fc21d4849a745e1e5dd81f5a05c767b1f52f78bd940058d578061/microsoft/TypeScript/issues/53690 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b6aadbbf5f1fc21d4849a745e1e5dd81f5a05c767b1f52f78bd940058d578061/microsoft/TypeScript/issues/53690 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | henrikeliq |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width