Title: Polymorphic "this" for static members · Issue #5862 · microsoft/TypeScript · GitHub
Open Graph Title: Polymorphic "this" for static members · Issue #5862 · microsoft/TypeScript
X Title: Polymorphic "this" for static members · Issue #5862 · microsoft/TypeScript
Description: When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting this when used in conjunction with a constructor or template/generic. I've posted before ab...
Open Graph Description: When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting this when used in conjunction with a constructor or...
X Description: When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting this when used in conjunction with a constructor or...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/5862
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Polymorphic \"this\" for static members","articleBody":"When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting `this` when used in conjunction with a constructor or template/generic.\n\nI've posted before about this here, #5493, and #5492 appears to mention this behavior also.\n\nAnd here is an SO post this that I made:\nhttp://stackoverflow.com/questions/33443793/create-a-generic-factory-in-typescript-unsolved\n\nI have recycled my example from #5493 into this ticket for further discussion. I don't have an immediate proposal for how to correct this but I wanted an open ticket representing the desire for such a thing.\n\nHere is an example that outlines a model `Factory` which produces models. If you want to customize the `BaseModel` that comes back from the `Factory` you should be able to override it. However this fails because `this` cannot be used in a static member.\n\n``` typescript\n// Typically in a library\nexport interface InstanceConstructor\u003cT extends BaseModel> {\n new(fac: Factory\u003cT>): T;\n}\n\nexport class Factory\u003cT extends BaseModel> {\n constructor(private cls: InstanceConstructor\u003cT>) {}\n\n get() {\n return new this.cls(this);\n }\n}\n\nexport class BaseModel {\n\n constructor(private fac: Factory\u003cthis>) {}\n\n refresh() {\n // get returns a new instance, but it should be of\n // type Model, not BaseModel.\n return this.fac.get();\n }\n}\n\n// Application Code\nexport class Model extends BaseModel {\n do() {\n return true;\n }\n}\n\n// Kinda sucks that Factory cannot infer the \"Model\" type\nlet f = new Factory\u003cModel>(Model);\nlet a = f.get();\n\nlet b = a.refresh();\n```\n","author":{"url":"https://github.com/tlong-dev","@type":"Person","name":"tlong-dev"},"datePublished":"2015-12-01T20:19:16Z","url":"https://github.com/microsoft/TypeScript/issues/5862"}
| 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:4af8df47-a1c8-f61e-ae38-cb599bbd70de |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88A6:22DB35:F6DE29:13DB3A7:6B00FF09 |
| html-safe-nonce | 51dcfe75c9dd5e4152e18dc292af7052e2c21de544ac813de04eee263be63c11 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEE2OjIyREIzNTpGNkRFMjk6MTNEQjNBNzo2QjAwRkYwOSIsInZpc2l0b3JfaWQiOiIyNDUxNjcwNTM4MTIxMTgyOTg1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 87fce5ede75e307036ba898cc9e8463cdbae44bb20446897b5f518a2cbb985b3 |
| hovercard-subject-tag | issue:119799232 |
| 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/5862/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e12f16347ad2ff217bd9380063196c2084e99bbfd751cdbb08e7c5a7e803952a/microsoft/TypeScript/issues/5862 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e12f16347ad2ff217bd9380063196c2084e99bbfd751cdbb08e7c5a7e803952a/microsoft/TypeScript/issues/5862 |
| og:image:alt | When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting this when used in conjunction with a constructor or... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tlong-dev |
| 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