Title: Consider inferring class members types by implemented interface members (continuation of #340) · Issue #32082 · microsoft/TypeScript · GitHub
Open Graph Title: Consider inferring class members types by implemented interface members (continuation of #340) · Issue #32082 · microsoft/TypeScript
X Title: Consider inferring class members types by implemented interface members (continuation of #340) · Issue #32082 · microsoft/TypeScript
Description: Continuation of #340, #1373, #5749, #6118, #10570, #16944, #23911. Why a new issue? Many previous issues discussed about having contextual types based on both extended base class members and implemented interface members. This proposal o...
Open Graph Description: Continuation of #340, #1373, #5749, #6118, #10570, #16944, #23911. Why a new issue? Many previous issues discussed about having contextual types based on both extended base class members and implem...
X Description: Continuation of #340, #1373, #5749, #6118, #10570, #16944, #23911. Why a new issue? Many previous issues discussed about having contextual types based on both extended base class members and implem...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32082
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Consider inferring class members types by implemented interface members (continuation of #340)","articleBody":"Continuation of #340, #1373, #5749, #6118, #10570, #16944, #23911.\r\n\r\n## Why a new issue?\r\n\r\nMany previous issues discussed about having contextual types based on **both** extended base class members **and** implemented interface members. This proposal only applies to `implements`, not `extends`.\r\n\r\nDiscussions in #6118 [ends](https://github.com/microsoft/TypeScript/pull/6118#issuecomment-216595207) with an edge case when a class extends a base class and implements another interface. I think this problem would not occur if the scope is limited to `implements` keyword only, and I believe would be a step forward from having parameters inferred as `any`.\r\n\r\nPrevious issues have been locked, making it impossible to continue the discussion as time passes.\r\n\r\n## Proposal\r\n\r\nUsing terminology “option 1”, “option 2”, “option 3” referring to this comment: https://github.com/microsoft/TypeScript/issues/10570#issuecomment-296860943\r\n\r\n- For members from `extends` keep option 1.\r\n- For members from `implements`:\r\n - Use option 3 for non-function properties that don’t have type annotation.\r\n - Use option 2 for function properties and methods.\r\n\r\n## Examples\r\n\r\nCode example in the [linked comment](https://github.com/microsoft/TypeScript/pull/6118#issuecomment-216595207):\r\n\r\n```ts\r\n class C extends Base.Base implements Contract {\r\n item = createSubitem(); // ⬅️ No type annotation -- inferred as `Subitem`\r\n }\r\n```\r\n\r\nSince only the `implements` keyword is considered, `item` will be inferred as `Subitem`.\r\n\r\nExample in https://github.com/microsoft/TypeScript/issues/10570#issuecomment-296860943\r\n\r\n```ts\r\ninterface I {\r\n kind: 'widget' | 'gadget';\r\n}\r\n\r\nclass C implements I {\r\n kind = 'widget'; // ⬅️ No type annotation -- inferred as 'widget' | 'gadget'\r\n}\r\n\r\n// Above behavior is consistent with:\r\nconst c: I = {\r\n kind: 'widget' // ⬅️ c.kind is also 'widget' | 'gadget'\r\n}\r\n```\r\n\r\n```ts\r\ninterface I {\r\n kind: 'widget' | 'gadget';\r\n}\r\n\r\nclass C implements I {\r\n kind: 'widget' = 'widget'; // ⬅️ Explicit type annotation required to pin as 'widget'\r\n}\r\n```\r\n\r\nExample in #16944:\r\n\r\n```ts\r\ninterface IComponentLifecycle\u003cP> {\r\n componentWillReceiveProps?(nextProps: Readonly\u003cP>, nextContext: any): void;\r\n}\r\n\r\ninterface IProps {\r\n hello: string;\r\n}\r\n\r\nclass X implements IComponentLifecycle\u003cIProps> {\r\n componentWillReceiveProps(nextProps) {\r\n // ^ Contextually typed as Readonly\u003cIProps>\r\n }\r\n}\r\n```\r\n\r\nExample in #340:\r\n\r\n```ts\r\ninterface SomeInterface1 {\r\n getThing(x: string): Element;\r\n}\r\n\r\ninterface SomeInterface2 {\r\n getThing(x: number): HTMLElement; \r\n}\r\n\r\ndeclare class SomeClass implements SomeInterface1, SomeInterface2 {\r\n getThing(x) {\r\n // ^ Contextually inferred from\r\n // (SomeInterface1 & SomeInterface2)['getThing']\r\n // As of TS 3.5, it is an implicit any.\r\n }\r\n}\r\n\r\n// Above behavior is consistent with:\r\nconst c: SomeInterface1 & SomeInterface2 = {\r\n getThing(x) {\r\n // ^ Implicit any, as of TS 3.5\r\n },\r\n}\r\n```\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","author":{"url":"https://github.com/dtinth","@type":"Person","name":"dtinth"},"datePublished":"2019-06-25T08:34:45Z","url":"https://github.com/microsoft/TypeScript/issues/32082"}
| 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:f864598a-aab2-6791-4e4c-6ff6f8b894f9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB1C:FC5BE:E17B54:12C22D7:6B01055D |
| html-safe-nonce | 4e77beb271eb1c04a7ccebb211fbde47676aec7a49151553c776ca4b88a3de81 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjFDOkZDNUJFOkUxN0I1NDoxMkMyMkQ3OjZCMDEwNTVEIiwidmlzaXRvcl9pZCI6IjE2ODY2OTA0MTY5MjcyNDU2NjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bd853bcc89e9f89ec72972a3cde62c1cc09800d2c8c80a7c31cd5b8db2dc7b85 |
| hovercard-subject-tag | issue:460286358 |
| 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/32082/issue_layout |
| twitter:image | https://opengraph.githubassets.com/955877f915237b48038bbeb05705641257b4c551a60e937a5aa8d1fd29b1c416/microsoft/TypeScript/issues/32082 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/955877f915237b48038bbeb05705641257b4c551a60e937a5aa8d1fd29b1c416/microsoft/TypeScript/issues/32082 |
| og:image:alt | Continuation of #340, #1373, #5749, #6118, #10570, #16944, #23911. Why a new issue? Many previous issues discussed about having contextual types based on both extended base class members and implem... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dtinth |
| 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