Title: Don't require to implement optional abstract properties · Issue #40635 · microsoft/TypeScript · GitHub
Open Graph Title: Don't require to implement optional abstract properties · Issue #40635 · microsoft/TypeScript
X Title: Don't require to implement optional abstract properties · Issue #40635 · microsoft/TypeScript
Description: Search Terms abstract optional override Suggestion If in abstract class abstract property is marked as optional, allow child classes not to implement it. So I suggest to remove an error for property x of class D in the following code: ht...
Open Graph Description: Search Terms abstract optional override Suggestion If in abstract class abstract property is marked as optional, allow child classes not to implement it. So I suggest to remove an error for propert...
X Description: Search Terms abstract optional override Suggestion If in abstract class abstract property is marked as optional, allow child classes not to implement it. So I suggest to remove an error for propert...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/40635
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Don't require to implement optional abstract properties","articleBody":"## Search Terms\r\nabstract optional override\r\n\r\n## Suggestion\r\n\r\nIf in abstract class abstract property is marked as optional, allow child classes not to implement it.\r\nSo I suggest to remove an error for property `x` of class `D` in the following code:\r\n\r\nhttps://www.typescriptlang.org/play?ts=4.0.2#code/IYIwzgLgTsDGEAJYBthjAgggg3gKAUIQAcoB7CAU3koBMFRIZ4EpLhayA7ZATwQAeAfgBcCLgFcAtiEpQCRUhWpV6jaHERsO3Pgl5jJMuQgA+CCV1qUAZgEsudPAsLEJIZHdgJOAZSkQABYAFACUuC5EhLDcYGTIlAB0yGQA5sFBdmCJAgA0CJnZvKGRAL545XgoaBgAIgiUAlRWGNj4UUpUNPTanDz8-AC8CACMAAwVzo4A7gi1YYl+ASGhQA\r\n\r\n```ts\r\nabstract class A {\r\n protected abstract readonly x?: number\r\n protected abstract readonly y: number | undefined\r\n\r\n public doSmth() {\r\n console.log(this.x, this.y)\r\n }\r\n}\r\n\r\nclass D extends A {\r\n protected readonly y = 10\r\n}\r\n\r\nnew D().doSmth()\r\n```\r\n\r\nBut property `y` still must be implemented.\r\n\r\nNote that we already can do some sort of it\r\n\r\nhttps://www.typescriptlang.org/play?ts=4.0.2#code/JYOwLgpgTgZghgYwgAgJLIN4Chm+QBygHt9owBPAYThAGUBrYfAfgC5kQBXAWwCNoceQiTLkAspwDOYVN3wAbCNwjh2XPtGQAfZJxAATCDFAR9g3OeTKwACyL7qdRiwAUASnYA3IsDN4rELb2EtKyCkoqYO5ePmYAvlhYcLzSUIhgyAjycJKSyACCmJbCpFAUjgxMbBw8-FCWyanpBMSlFCEycorKqjUaUNq6BkYmfniNYGkIGSWiHWHdkQAqRETV6nWJ-tZ2DjSVrh7I3r4NKZPNO8FSneE9UUcnY7gTUxlX+vNdEeAra9HHWJYBJYLI5PKUQoQAAekAMeUK2AA9AAqSwAOSIIAAtK9mmDcsgAOSQonIfRECB5EBEDLAb73ZCgGzQYCQfTIPHTAL9YkfL53SJkmDEbiZbKEon5IkAOhcACYAKwARkVbgxWNx5ze4vBxNJ5Mp1NpTIZkSZIBZUDZpk52suSjqfMCuwFi1+q2Fot1kulcqVqvV-kxOK5GQJeRJ0sNVI4JvpgvAFqtNo5YZ5TqJszK4huCx+YC9RDFEeJfoVKrVGtD9u5pajZIpsZpdLNSeZrPZdqa3OUvKzrTmebbYD+RZLEsj5YDVZRSOBiVLlHQCfdYDy6GRaP8lEn+tQZNACCIUCgEGm8nIpsT64tkFgiBQRIPMssS3IpH3h7y3GAuVAADmyC2CgMBEPI8hEAA7oBLQiGUwCxiKxbAR+T4Huw2btMON4ADQBEEnw4WuCoACzygADOqc7AkAA\r\n\r\n```ts\r\ninterface I {\r\n propertyCanSkip?: number\r\n propertyMustImplement: number | undefined\r\n \r\n methodCanSkip?(): void\r\n methodMustImplement(): void\r\n}\r\n\r\nabstract class A {\r\n propertyCanSkip?: number\r\n abstract propertyMustImplement: number | undefined\r\n abstract propertyMustImplementToo?: number\r\n\r\n methodCanSkip?(): void\r\n abstract methodMustImplement(): void\r\n abstract methodMustImplementToo?(): void\r\n}\r\n\r\nclass CA extends A {\r\n/*\r\n Non-abstract class 'CA' does not implement inherited abstract member 'methodMustImplement' from class 'A'.(2515)\r\n Non-abstract class 'CA' does not implement inherited abstract member 'methodMustImplementToo' from class 'A'.(2515)\r\n Non-abstract class 'CA' does not implement inherited abstract member 'propertyMustImplement' from class 'A'.(2515)\r\n Non-abstract class 'CA' does not implement inherited abstract member 'propertyMustImplementToo' from class 'A'.(2515)\r\n*/\r\n}\r\n\r\nclass CI implements I {\r\n/*\r\n Class 'CI' incorrectly implements interface 'I'.\r\n Type 'CI' is missing the following properties from type 'I': propertyMustImplement, methodMustImplement(2420)\r\n*/\r\n}\r\n```\r\n\r\n## But there is a set of problems for properties\r\n\r\nWe have two ways to override property (via property declaration or via getter and setter). And now (in TS4) the limitation have changed. For nonabstract property base class always defines how it should be implemented in children.\r\n\r\nLet's look what implementations are possible (don't forget about `useDefineForClassFields` compiler flag that makes it more important):\r\n\r\nCode | Property can be omitted | Child can implement as property | Child can implement as get/set\r\n------------ | ------------- | ------------- | -------------\r\n`propertyCanSkip?: number` | Yes | Yes | No\r\n`abstract propertyMustImplement: number \\| undefined` | No | Yes | Yes (except https://github.com/microsoft/TypeScript/issues/40632)\r\n`abstract propertyMustImplementToo?: number` | No | Yes | Yes (except https://github.com/microsoft/TypeScript/issues/40632)\r\n`get getter?(): number` | N/A | N/A | N/A\r\n`abstract get getterMustImplement(): number \\| undefined` | No | No | Yes\r\n`abstract getterToo?(): number` | N/A | N/A | N/A\r\n\r\nIt's easy to see, that if the property should really be optional, there is only one way to make it such which will not allow to implement it as getter and setter. But we have 2 absolutely identical lines with optional and nonoptional abstract property. I see no sense for them to be synonyms as `?` in the 3rd line definitely says that the property _is optional_, but doesn't give me ability to make so in further code.\r\n\r\nSo I propose to change this table in following way:\r\n\r\nCode | Property can be omitted | Child can implement as property | Child can implement as get/set\r\n------------ | ------------- | ------------- | -------------\r\n`propertyCanSkip?: number` | Yes | Yes | No\r\n`abstract propertyMustImplement: number \\| undefined` | No | Yes | Yes\r\n`abstract propertyCanSkipToo?: number` | **Yes** | Yes | Yes\r\n`get getter?(): number` | N/A | N/A | N/A\r\n`abstract get getterMustImplement(): number \\| undefined` | No | No | Yes\r\n`abstract get getterCanSkipToo?(): number` | **Yes** | **No** | **Yes**\r\n\r\nAbstract getter is NOT a part of this feature request, just shown for consistency.\r\n\r\n## Use Cases\r\n\r\nProvide ability to list and use for reading an optional property in abstract class without limiting a way of its implementation in child classes. Such [problem occured](https://stackoverflow.com/q/63937846/4928642) in a real project because of migration from TS3 to TS4. Before that it was possible, but because of breaking changes of TS4 it's not anymore.\r\n\r\nhttps://www.typescriptlang.org/play?ts=4.0.2#code/IYIwzgLgTsDGEAJYBthjAgggg3gKAUIQAcoB7CAU3koBMEpLhayA7ZATwTAFsIALAPwAuBKwCuPEJSh4CRYuJDIAlrAQAzABQBKXPKJFYbMGWSUAdMjIBzLQJVgLvAToMIAvni94UaDABCCJQAHlSstBjY+IYu-AgAvAgAjAAM3nJ+6AgAwsFhlBFR+oY2lIhxurgM5eJQrAgAssACFjARZDxVXj5ZGAAi+eGRWPo+rJQA7ggBuhbabhPTOXMLeEsI-au6QA\r\n\r\n```ts\r\nabstract class A {\r\n protected readonly smth?: number\r\n\r\n public f() {\r\n console.log(this.smth)\r\n }\r\n}\r\n\r\nclass B extends A {\r\n smth = 10\r\n}\r\n\r\nclass C extends A {\r\n get smth() { return Math.random() }\r\n}\r\n\r\nclass D extends A {\r\n}\r\n\r\nnew B().f()\r\nnew C().f()\r\nnew D().f()\r\n```\r\n\r\n## Examples\r\n\r\nSee above.\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).\r\n\r\n## Why it's not a breaking change?\r\n\r\nIt changes behavior of existing construction, but it's not a breaking change in terms of code.\r\n\r\nIf you had your code working and you have\r\n\r\n abstract propertyMustImplementToo?: number\r\n\r\nin it, that means that you implemented this property in all child classes. So after its meaning changes all you code keeps being valid and compiles into absolutely the same javascript code as before. **Nothing changed.**\r\n\r\nAt the same time, for further development you have to decide whether you want to allow child classes to skip the property or not. If yes, or you don't care - keep it with `?` as it is. If no then update it to\r\n\r\n abstract propertyMustImplementToo: number | undefined\r\n\r\nwithout any other changes needed.\r\n\r\n## Related Issues:\r\nhttps://github.com/microsoft/TypeScript/issues/6413\r\nhttps://github.com/microsoft/TypeScript/issues/22939","author":{"url":"https://github.com/Qwertiy","@type":"Person","name":"Qwertiy"},"datePublished":"2020-09-18T17:28:28Z","url":"https://github.com/microsoft/TypeScript/issues/40635"}
| 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:7f36eeb6-8ff9-9121-2aee-5c5f9dae00af |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E28:EA06E:38930C3:47EA0FC:6B01DA9A |
| html-safe-nonce | 62f9909a136c9c0ee7c20fc9197382e00642ecbcfdf51b2364f4d9066ac4c82b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTI4OkVBMDZFOjM4OTMwQzM6NDdFQTBGQzo2QjAxREE5QSIsInZpc2l0b3JfaWQiOiIyMDM0MzMyMDQwNTk3NDYxNjU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a8b326c89c12d577ff7bb3bea235a4f5e04e699e612c461d3e35c226ebefa86c |
| hovercard-subject-tag | issue:704540482 |
| 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/40635/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6a0893a2395682e01ac45ff30ac002fa01ef532d2d913f6290259942d9e06f94/microsoft/TypeScript/issues/40635 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6a0893a2395682e01ac45ff30ac002fa01ef532d2d913f6290259942d9e06f94/microsoft/TypeScript/issues/40635 |
| og:image:alt | Search Terms abstract optional override Suggestion If in abstract class abstract property is marked as optional, allow child classes not to implement it. So I suggest to remove an error for propert... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Qwertiy |
| 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