Title: Allow types inside of class · Issue #62610 · microsoft/TypeScript · GitHub
Open Graph Title: Allow types inside of class · Issue #62610 · microsoft/TypeScript
X Title: Allow types inside of class · Issue #62610 · microsoft/TypeScript
Description: 🔍 Search Terms internal types, types within class, type class scope ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code Thi...
Open Graph Description: 🔍 Search Terms internal types, types within class, type class scope ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime ...
X Description: 🔍 Search Terms internal types, types within class, type class scope ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/62610
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow types inside of class","articleBody":"### 🔍 Search Terms\n\ninternal types, types within class, type class scope\n\n### ✅ Viability Checklist\n\n- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [x] This wouldn't change the runtime behavior of existing JavaScript code\n- [x] This could be implemented without emitting different JS based on the types of the expressions\n- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types\n- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nAllow `type` expression within class, scoped to class.\n\nEg.\n\n```ts\nclass Foo\u003cBar, Baz> {\n type BarBazTuple = [Bar, Baz];\n \n\n method(arg: BarBazTuple /* Allowed here */) {\n return 42;\n }\n}\n\nclass Bar extends Foo\u003cnumber, string> {\n methodB(arg: super.BarBazTuple /* [number, string] */) {}\n}\n\n\nconst v: Foo\u003cnumber,string>['BarBazTuple'] = [1,'a'];\n```\n\nAnother example are more complicated constraints, eg:\n\n```\nexport class Foo\u003cT extends {$discriminator: string}> {\n method(v: T extends {'$discriminator': 'special'} ? X\u003cT> : T);\n}\n```\n\nThese are quick to stack, and library authors often believe that no one will need to use these types.\n\n### 📃 Motivating Example\n\nThis feature would allow library authors to trivially namespace internal types without cluttering exports and devising complicated names.\n\nEg. application that I'm working right now uses:\n\n```ts\n public async method(payload: unknown): Promise\u003cConstructorParameters\u003ctypeof StreamableFile>[0]> {\n``` \n\nSuch feature would allow library authors to default (by code convention) to exporting all publicly used types.\n\n### 💻 Use Cases\n\n1. What do you want to use this for?\n\nI want to keep types grouped with their \"declarators\". Eg. \n```ts\nexport class RedisConfigurationOptions {\n type Input = {foo?: string; whatever: number | bigint | `${number}`;}\n type Normalized = {foo: AsciiStringWrapper, whatever: `${number}`;};\n\n private readonly options: Normalized;\n constructor(input: Input) {\n this.options = this.normalizeInput(input);\n }\n}\n```\n\n\n2. What shortcomings exist with current approaches?\n\nIt generates long type names, and library developers don't know which types their users will want.\n\nEg. previous example would likely be written as:\n\n\n```ts\n\ntype RedisConfigurationOptionsInput = {foo?: string; whatever: number | bigint | `${number}`;}\n\ntype RedisConfigurationNormalizedOptions = {foo: AsciiStringWrapper, whatever: `${number}`;};\n\nexport class RedisConfigurationOptions {\n\n private readonly options: Normalized;\n constructor(input: Input) {\n this.options = this.normalizeInput(input);\n }\n}\n```\n\nSo any code eg. building options depending on enviroment variables would have to use `ConstructorParameters\u003ctypeof RedisConfigurationOptions>[0]`... Or just throw `any`, as is extremely common for code in the wild.","author":{"url":"https://github.com/Ginden","@type":"Person","name":"Ginden"},"datePublished":"2025-10-16T09:25:03Z","url":"https://github.com/microsoft/TypeScript/issues/62610"}
| 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:52acfbc0-693c-718a-bc3a-65a5181fc8fd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D528:395430:E3C6AF:12D856A:6B00FEE1 |
| html-safe-nonce | 52606ad197bdc5c5669753b377746876960678096e9b0eb194c33d2ed94f3b95 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTI4OjM5NTQzMDpFM0M2QUY6MTJEODU2QTo2QjAwRkVFMSIsInZpc2l0b3JfaWQiOiIzODYyNjA5ODE1MDMzNDEzMzQ1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | de3dda93103190e31bbb784276932eab72d26abb8f0bb2e989fd7c2b6b4fa340 |
| hovercard-subject-tag | issue:3521049998 |
| 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/62610/issue_layout |
| twitter:image | https://opengraph.githubassets.com/846dff92ec6f2fe97220377eefd5bc5e47cbd6516bf51f688305f3a5a2167540/microsoft/TypeScript/issues/62610 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/846dff92ec6f2fe97220377eefd5bc5e47cbd6516bf51f688305f3a5a2167540/microsoft/TypeScript/issues/62610 |
| og:image:alt | 🔍 Search Terms internal types, types within class, type class scope ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Ginden |
| 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