Title: `static abstract` methods and properties · Issue #34516 · microsoft/TypeScript · GitHub
Open Graph Title: `static abstract` methods and properties · Issue #34516 · microsoft/TypeScript
X Title: `static abstract` methods and properties · Issue #34516 · microsoft/TypeScript
Description: This is a continuation of #14600 which had two separate features proposed in the same issue (static members in interfaces and abstract static class members) Search Terms static abstract method property properties implement concrete Sugge...
Open Graph Description: This is a continuation of #14600 which had two separate features proposed in the same issue (static members in interfaces and abstract static class members) Search Terms static abstract method prop...
X Description: This is a continuation of #14600 which had two separate features proposed in the same issue (static members in interfaces and abstract static class members) Search Terms static abstract method prop...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34516
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`static abstract` methods and properties","articleBody":"This is a continuation of #14600 which had two separate features proposed in the same issue (static members in interfaces *and* abstract static class members)\r\n\r\n## Search Terms\r\n\r\nstatic abstract method property properties implement concrete\r\n\r\n## Suggestion\r\n\r\nCurrently, this code is illegal:\r\n```ts\r\nabstract class A {\r\n static abstract doSomething(): void;\r\n}\r\n\r\n// Should be OK\r\nclass B extends A {\r\n static doSomething() { }\r\n}\r\n\r\n// Should be an error; non-abstract class failed to implement abstract member\r\nclass C extends A {\r\n\r\n}\r\n```\r\n\r\nIt should be legal to have `abstract static` (`static abstract`?) members.\r\n\r\n## Use Cases\r\n\r\n(what are they?)\r\n\r\n## Unresolved Questions\r\n\r\n**What calls of abstract static methods are allowed?**\r\n\r\nLet's say you wrote a trivial hierarchy\r\n```ts\r\nabstract class A {\r\n static abstract doSomething(): void;\r\n}\r\nclass B extends A {\r\n static doSomething() { }\r\n}\r\n```\r\n\r\nFor an expression `x.doSomething()`, what are valid `x`s?\r\n\r\n#### Option 1: All of them\r\n\r\nBecause `this` isn't generic in `static` members, we should simply allow all invocations of abstract static methods. Otherwise code like this would be illegal:\r\n```ts\r\nabstract class A {\r\n static abstract initialize(self: A): void;\r\n static createInstance() {\r\n const a = new this();\r\n this.initialize(a);\r\n return a;\r\n }\r\n}\r\n```\r\n\r\nHowever, this means that TypeScript would miss straight-up crashes:\r\n```ts\r\n// Exception: 'this.initialize' is not a function\r\nA.createInstance();\r\n```\r\n\r\n * **Pros**: Ergonomic\r\n * **Cons**: Literally allows the runtime-crashing code `A.doSomething()`, which seems like a fairly large design deficit\r\n\r\n#### Option 2: None of them\r\n\r\nAllowing crashes is bad, so the rule should be that `static abstract` methods simply don't exist from a type system perspective except to the extent that they enforce concrete derived class constraints:\r\n\r\n```ts\r\nabstract class A {\r\n static abstract doSomething(): void;\r\n}\r\nclass B extends A {\r\n static doSomething() { }\r\n}\r\n\r\n// Error, can't call abstract method\r\nA.doSomething();\r\n\r\n// This call would work, but it'd still be an error\r\nconst Actor: typeof A = B;\r\nActor.doSomething();\r\n\r\nfunction indirect(a: { doSomething(): void }) {\r\n a.doSomething();\r\n}\r\n\r\n// Error, can't use abstract method 'doSomething' to satisfy concrete property\r\nindirect(A);\r\n// OK\r\nindirect(B);\r\n```\r\n\r\nThis is unergonomic because it'd be impossible to write a function that dealt with an arbitrary complex constructor function without tedious rewriting:\r\n```ts\r\nabstract class Complicated {\r\n static abstract setup(): void;\r\n static abstract print(): void;\r\n static abstract ship(): void;\r\n static abstract shutdown(): void;\r\n}\r\n\r\nfunction fn(x: typeof Complicated) {\r\n // Error, can't call abstract method\r\n x.setup();\r\n // Error, can't call abstract method\r\n x.print();\r\n // Error, can't call abstract method\r\n x.ship();\r\n // Error, can't call abstract method\r\n x.shutdown();\r\n}\r\n```\r\n\r\nWe know this is a problem because people get tripped up by it constantly when they try to `new` an abstract class:\r\n\r\nhttps://www.reddit.com/r/typescript/comments/bcyt07/dynamically_creating_instance_of_subclass/\r\nhttps://stackoverflow.com/questions/57402745/create-instance-inside-abstract-class-of-child-using-this\r\nhttps://stackoverflow.com/questions/49809191/an-example-of-using-a-reference-to-an-abstract-type-in-typescript\r\nhttps://stackoverflow.com/questions/53540944/t-extends-abstract-class-constructor\r\nhttps://stackoverflow.com/questions/52358162/typescript-instance-of-an-abstract-class\r\nhttps://stackoverflow.com/questions/53692161/dependency-injection-of-abstract-class-in-typescript\r\nhttps://stackoverflow.com/questions/52358162/typescript-instance-of-an-abstract-class\r\n\r\nFor `abstract` constructor signatures, the recommended fix of using `{ new(args): T }` is pretty good because a) you need to be explicit about what arguments you're actually going to provide anyway and b) there's almost always exactly one signature you care about, but for `static abstract` methods/properties this is much more problematic because there could be *any* number of them.\r\n\r\nThis also would make it impossible for concrete `static` methods to invoke `abstract static` methods:\r\n```ts\r\nabstract class A {\r\n static abstract initialize(self: A): void;\r\n static createInstance() {\r\n const a = new this();\r\n // Error\r\n this.initialize(a);\r\n return a;\r\n }\r\n}\r\n```\r\nOn the one hand, this is *good*, because `A.createInstance()` definitely *does* crash. On the other hand, this literally the exact kind of code you *want* to write with abstract methods.\r\n\r\nOne solution would be the existence of an `abstract static` method *with a body*, which would be allowed to invoke other `abstract static` methods but would be subject to invocation restrictions but *not* require a derived class implementation. This is also confusing because it would seem like this is just a \"default implementation\" that would still require overriding (that is the bare meaning of `abstract`, after all):\r\n```ts\r\nabstract class A {\r\n abstract static initialize() {\r\n console.log(\"Super class init done; now do yours\");\r\n }\r\n}\r\n// No error for failing to provide `static initialize() {`, WAT?\r\nclass B extends A { }\r\n```\r\n\r\nAn alternative would be to say that you can't call *any* `static` method on an `abstract` class, even though that would ban trivially-OK code for seemingly no reason:\r\n```ts\r\nabstract class A {\r\n static foo() { console.log(\"Everything is fine\"); }\r\n}\r\n// Can't invoke, WAT?\r\nA.foo();\r\n```\r\n\r\n * **Pros**: Correctly prevents all crashes\r\n * **Cons**: Extremely unergonomic at use cases; effectively bans concrete `static` methods from calling same-class `abstract` methods\r\n\r\n#### Option 3: Indirection is sufficient\r\n\r\nWhy not just split the baby and say that the *direct* form `A.doSomething()` is illegal, but `expr.doSomething()` where `expr` is of type `typeof A` is OK as long as `expr` isn't *exactly* `A`.\r\n\r\nThis creates the *dread inconsistency* that a trivial indirection is sufficient to defeat the type system and cause a crash:\r\n```ts\r\n// Error; crash prevented!\r\nA.doSomething();\r\nconst p = A;\r\n// OK, crashes, WAT?\r\np.doSomething();\r\n```\r\n\r\nIt's also not entirely clear what \"indirection\" means. Technically if you write\r\n```ts\r\nimport { SomeStaticAbstractClass as foo } from \"./otherModule\";\r\nfoo.someAbstractMethod();\r\n```\r\nthen `foo` isn't *exactly* the declaration of SomeStaticAbstractClass itself - it's an alias. But there isn't really anything distinguishing that from `const p = A` above.\r\n\r\n * **Pros**: Catches \"bad by inspection\" instances while still allowing \"maybe it works\" code\r\n * **Cons**: Extremely inconsistent; simply appears to function as if TypeScript has a bug in it. Unclear what sufficient indirection means in cases of e.g. module imports\r\n\r\n#### Option 4: Indirection, but with generics\r\n\r\nMaybe a trivial indirection as described in Option 3 isn't \"good enough\" and we should require you to use a constrained generic instead:\r\n```ts\r\n// Seems like you're maybe OK\r\nfunction fn\u003cT extends typeof A>(x: T) {\r\n x.doSomething();\r\n}\r\n\r\n// Good, OK\r\nfn(B);\r\n// A fulfills typeof A, fair enough, crashes, WAT?\r\nfn(A);\r\n```\r\n\r\nThis turns out to be a bad option because many subclasses don't actually meet their base class static constraints due to constructor function arity differences:\r\n```ts\r\nabstract class A {\r\n constructor() { }\r\n foo() { }\r\n}\r\n\r\nclass B extends A {\r\n constructor(n: number) {\r\n super();\r\n }\r\n bar() { }\r\n}\r\n\r\nfunction fn\u003cT extends typeof A>(ctor: T) {\r\n // Want to use static methods of 'ctor' here\r\n}\r\n// Error, B's constructor has too many args\r\nfn(B);\r\n```\r\nThis isn't even code we *want* people to write -- a generic type parameter used in exactly one position is something we explicitly discourage because it doesn't \"do anything\".\r\n\r\n * **Pros**: Maybe a slightly better variant of option 3\r\n * **Cons**: Just a more complicated system with the same failure modes\r\n\r\n#### Option 5: Something else?\r\n\r\nAnyone able to square this circle?\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] 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, 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).","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2019-10-16T16:42:38Z","url":"https://github.com/microsoft/TypeScript/issues/34516"}
| 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:d6f0e0a7-86b1-e541-6a2d-cf52a19650b4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFB8:3F4253:26C9866:33A9C08:6B01AB32 |
| html-safe-nonce | 7515098b2d8e08c970a2712e919b45a99e2096286c2f64cf8825ae33f3359d18 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkI4OjNGNDI1MzoyNkM5ODY2OjMzQTlDMDg6NkIwMUFCMzIiLCJ2aXNpdG9yX2lkIjoiMjkyMzIxMTE5MjIwODA0MjgwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 254fa4783345b378932c08e30d3ff8a6e14414e49b5bbdcac585541684884389 |
| hovercard-subject-tag | issue:507967613 |
| 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/34516/issue_layout |
| twitter:image | https://opengraph.githubassets.com/88e4eb675dfafcc633be5e6cdd36acccb69a7f1a48b5d6c4c939ffda7c06ac58/microsoft/TypeScript/issues/34516 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/88e4eb675dfafcc633be5e6cdd36acccb69a7f1a48b5d6c4c939ffda7c06ac58/microsoft/TypeScript/issues/34516 |
| og:image:alt | This is a continuation of #14600 which had two separate features proposed in the same issue (static members in interfaces and abstract static class members) Search Terms static abstract method prop... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width