Title: Allow object types to have property-like associated types · Issue #17588 · microsoft/TypeScript · GitHub
Open Graph Title: Allow object types to have property-like associated types · Issue #17588 · microsoft/TypeScript
X Title: Allow object types to have property-like associated types · Issue #17588 · microsoft/TypeScript
Description: Edit: Add static Type variant for classes, make abstractness a little clearer, clarify things. This has probably already been asked before, but a quick search didn't turn up anything. This is similar, and partially inspired by, Swift's/R...
Open Graph Description: Edit: Add static Type variant for classes, make abstractness a little clearer, clarify things. This has probably already been asked before, but a quick search didn't turn up anything. This is simil...
X Description: Edit: Add static Type variant for classes, make abstractness a little clearer, clarify things. This has probably already been asked before, but a quick search didn't turn up anything. This is s...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17588
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow object types to have property-like associated types","articleBody":"**Edit:** Add `static Type` variant for classes, make abstractness a little clearer, clarify things.\r\n\r\n*This has probably already been asked before, but a quick search didn't turn up anything.*\r\n\r\nThis is similar, and partially inspired by, Swift's/Rust's associated types and Scala's abstract types.\r\n\r\n---\r\n\r\n## Rationale\r\n\r\n- In complex structures like in virtual DOM components/nodes and in graphs, where there are numerous constraints to check, but they would get cumbersome in a hurry to force off onto the user.\r\n- ~~In dynamic imports, where you never have the module namespace itself until runtime, yet you still want to be able to use types defined within it.~~ **Edit:** [Already addressed elsewhere](https://github.com/Microsoft/TypeScript/issues/22445).\r\n- In types where there's a *lot* of optional parameters, it'd be easier and more flexible to label which ones you're defining as part of the type.\r\n- In types where you want to specify a single optional parameter, and the required ordering requires you to specify some that you *don't* want to specify, that would get boilerplatey in a hurry. I've experienced this personally on multiple occasions.\r\n- It would allow fully typing namespaces and named module imports without hard-coded compiler handling.\r\n\r\n## Proposed Syntax/Semantics\r\n\r\n- Each interface and object type may have a number of associated types within it.\r\n- Associated types are checked for assignability like any other usual property, except they live in a different \"namespace\" from properties. In particular, the type must be assignable to the target's type.\r\n- To access an associated type, you use `TypeName.Type`, where `Type` is the name of an associated type.\r\n - For sugar and namespace compatibility, `object.Type` is equivalent to `(typeof object).Type`\r\n- To expect an interface with an associated type, you use `Foo & {type Type: Value}` or `Foo with \u003cType: Value>`.\r\n- To expect an object with an associated type, you use `{type Type: Value}`.\r\n- To declare an abstract associated type, you use `type Type: *` within the interface or object type.\r\n- To declare a non-abstract associated type, you use `type Type: Default` within the interface or object type.\r\n- To constrain an abstract associated type, you use `type Type: * extends Super`.\r\n- Associated types may be inherited from other interfaces and/or object types.\r\n- Namespaces' types are modified to include the exported types as associated types.\r\n- Classes may also define associated types, optionally with visibility modifiers.\r\n- Associated types may have defaults, in case they aren't defined or further constrained later.\r\n - Constraining an associated type with an existing default removes the default if and only if the existing constraint is not assignable to the new constraint. For example, `{type Foo: string | number = string} & {type Foo: string | number}` is assignable to `{type Foo: string}`, but `{type Foo: string | number = string} & {type Foo: number}` is not.\r\n\r\nHere's what that would look like in syntax:\r\n\r\n```ts\r\n// Interfaces\r\ninterface Foo {\r\n type Type: *; // abstract\r\n type Sub: * extends Type; // abstract, constrained\r\n type Type: Default;\r\n type Type: * extends Type = Default; // late-bound default\r\n}\r\n\r\n// Objects\r\ntype Foo = {\r\n type Type: Foo,\r\n}\r\n\r\n// Classes\r\nabstract class Foo {\r\n // Note: outer class *must* be abstract for these, and the keyword is required.\r\n abstract type Type: *; // abstract\r\n private abstract type Sub: * extends Type; // abstract, constrained\r\n protected abstract type Type: * extends Type = Default, // late-bound default\r\n\r\n // Note: outer class *may* be not abstract for these.\r\n type Type: Default;\r\n private type Type: Default;\r\n\r\n // Declare an associated type in the class\r\n // Note: type must not be abstract.\r\n static Type: Foo;\r\n}\r\n```\r\n\r\n## Emit\r\n\r\nThis has no effect on the JavaScript emit as it is purely type-level.\r\n\r\n## Compatibility\r\n\r\n- This is purely additive, making no observably incompatible changes beyond possibly different error messages.\r\n- This has no impact on any existing JavaScript-related proposal.\r\n\r\n## Other\r\n\r\n- It may slow down the type checker a little initially when namespaces are unified, but two optimization points are available, which would recover most of the perf hit, if not all:\r\n - Associated types could be stored in a per-interface type map.\r\n - The list of associated types could be initially stored as `undefined` to avoid generating a large number of empty arrays.\r\n- It should have little effect on editor tooling.","author":{"url":"https://github.com/dead-claudia","@type":"Person","name":"dead-claudia"},"datePublished":"2017-08-03T15:32:21Z","url":"https://github.com/microsoft/TypeScript/issues/17588"}
| 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:77505628-ebfb-d786-b3ae-b0ba875720a7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFEE:1AE20C:A47393:D1689D:6B00F1A6 |
| html-safe-nonce | 24889441b032e501b1213efb61da93a2333f8baea56bfa802d51391916968897 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkVFOjFBRTIwQzpBNDczOTM6RDE2ODlEOjZCMDBGMUE2IiwidmlzaXRvcl9pZCI6IjE1MTc4MjU5NTU1MjY3MDExOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0548305ede5fa04681859faa223669200d3f6be89d6c4b1165e2ecc11ca4140e |
| hovercard-subject-tag | issue:247747086 |
| 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/17588/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cfc90eef5665c7531cddc135ec4a918a15d7bf91924278e2d224cb0671937275/microsoft/TypeScript/issues/17588 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cfc90eef5665c7531cddc135ec4a918a15d7bf91924278e2d224cb0671937275/microsoft/TypeScript/issues/17588 |
| og:image:alt | Edit: Add static Type variant for classes, make abstractness a little clearer, clarify things. This has probably already been asked before, but a quick search didn't turn up anything. This is simil... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dead-claudia |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width