Title: Type alias circular reference does not error and causes implicit any · Issue #26104 · microsoft/TypeScript · GitHub
Open Graph Title: Type alias circular reference does not error and causes implicit any · Issue #26104 · microsoft/TypeScript
X Title: Type alias circular reference does not error and causes implicit any · Issue #26104 · microsoft/TypeScript
Description: TypeScript Version: typescript@next typescript@2.9.2 Search Terms: circular reference circular reference returntype Type alias circularly references itself returntype Type alias circularly references itself implicit any Code type A = Ret...
Open Graph Description: TypeScript Version: typescript@next typescript@2.9.2 Search Terms: circular reference circular reference returntype Type alias circularly references itself returntype Type alias circularly referenc...
X Description: TypeScript Version: typescript@next typescript@2.9.2 Search Terms: circular reference circular reference returntype Type alias circularly references itself returntype Type alias circularly referenc...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26104
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type alias circular reference does not error and causes implicit any","articleBody":"**TypeScript Version:** \r\n- `typescript@next`\r\n- `typescript@2.9.2`\r\n\r\n**Search Terms:**\r\n- `circular reference`\r\n- `circular reference returntype`\r\n- `Type alias circularly references itself returntype`\r\n- `Type alias circularly references itself implicit any`\r\n\r\n**Code**\r\n```ts\r\ntype A = ReturnType\u003cA>;\r\n// ^^^ Type alias 'A' circularly references itself.\r\n\r\ninterface Input {\r\n a: number;\r\n b: number;\r\n}\r\n\r\ntype R = ReturnType\u003ctypeof mul>;\r\nfunction mul(input: Input): R {\r\n return input.a * input.b;\r\n}\r\n// ^^^ Return value of `mul` is now `any` because of the circular reference\r\n```\r\n\r\n**Expected behavior:**\r\nExpected error: `Type alias 'R' circularly references itself.` (identical to `A`: `Type alias 'A' circularly references itself.`)\r\n\r\n**Actual behavior:**\r\nOnly error: `Type alias 'A' circularly references itself.` (nothing about `R` or `mul` and the circular type reference)\r\n\r\n**Reasoning:**\r\nIn this example, the `mul` function is simple but we use this for functions that can return complex objects (deeply nested) that already have explicit types. We then pass type `R` to other generics to provide type safety. This pattern works well, until we accidentally circular reference type `R` as in the above example. It's easy to miss and continue using because type `R` is now `any` so we (almost) never run into type errors as a result of this. \r\n\r\n**[Playground Link](https://www.typescriptlang.org/play/index.html#src=type%20A%20%3D%20ReturnType%3CA%3E%3B%0D%0A%2F%2F%20%20%5E%5E%5E%20Type%20alias%20'A'%20circularly%20references%20itself.%0D%0A%0D%0Ainterface%20Input%20%7B%0D%0A%20%20a%3A%20number%3B%0D%0A%20%20b%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Atype%20R%20%3D%20ReturnType%3Ctypeof%20mul%3E%3B%0D%0Afunction%20mul(input%3A%20Input)%3A%20R%20%7B%0D%0A%20%20return%20input.a%20*%20input.b%3B%0D%0A%7D%0D%0A%2F%2F%20%5E%5E%5E%20Return%20value%20of%20%60mul%60%20is%20now%20%60any%60%20because%20of%20the%20circular%20reference)**\r\n\r\n**Potentially Related Issues:**\r\n- https://github.com/Microsoft/TypeScript/issues/25609\r\n","author":{"url":"https://github.com/skovy","@type":"Person","name":"skovy"},"datePublished":"2018-07-31T21:38:23Z","url":"https://github.com/microsoft/TypeScript/issues/26104"}
| 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:ea9ca67f-ad7f-a472-ebdd-17152069c07a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A620:19AA62:2A4566F:35AF370:6B01A710 |
| html-safe-nonce | 50b10c3ec5aecc04ef100339c991188e164c3541c1d907bdedd6bd2793885054 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjIwOjE5QUE2MjoyQTQ1NjZGOjM1QUYzNzA6NkIwMUE3MTAiLCJ2aXNpdG9yX2lkIjoiMjUxODg3MzkzMDA1NjQ0MzY2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 57804abf9117a659df5e3d72e0e5367c5709a464bccb4207e6677c5c4b9e9388 |
| hovercard-subject-tag | issue:346368011 |
| 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/26104/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c6a700fdb75b856abe376b54c72bcfbc1d3bf21bc04d4e50f9c48218f69cb734/microsoft/TypeScript/issues/26104 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c6a700fdb75b856abe376b54c72bcfbc1d3bf21bc04d4e50f9c48218f69cb734/microsoft/TypeScript/issues/26104 |
| og:image:alt | TypeScript Version: typescript@next typescript@2.9.2 Search Terms: circular reference circular reference returntype Type alias circularly references itself returntype Type alias circularly referenc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | skovy |
| 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