Title: Recursive functions are inferred to have return type `any` · Issue #3336 · microsoft/TypeScript · GitHub
Open Graph Title: Recursive functions are inferred to have return type `any` · Issue #3336 · microsoft/TypeScript
X Title: Recursive functions are inferred to have return type `any` · Issue #3336 · microsoft/TypeScript
Description: Consider this code: function fact( x: number, acc?: number ) { if (x <= 1) return acc; else return fact( x - 1, (acc||1)*x ); } var res = fact( 5 ); (same thing on playground) Here, fact is inferred to have type (number, number?) => any,...
Open Graph Description: Consider this code: function fact( x: number, acc?: number ) { if (x <= 1) return acc; else return fact( x - 1, (acc||1)*x ); } var res = fact( 5 ); (same thing on playground) Here, fact is inferre...
X Description: Consider this code: function fact( x: number, acc?: number ) { if (x <= 1) return acc; else return fact( x - 1, (acc||1)*x ); } var res = fact( 5 ); (same thing on playground) Here, fact is infe...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3336
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Recursive functions are inferred to have return type `any`","articleBody":"Consider this code:\n\n```\nfunction fact( x: number, acc?: number ) {\n if (x \u003c= 1) return acc;\n else return fact( x - 1, (acc||1)*x );\n}\n\nvar res = fact( 5 );\n```\n\n([same thing on playground](http://www.typescriptlang.org/Playground/#src=%0Afunction%20fact%28%20x%3A%20number%2C%20acc%3F%3A%20number%20%29%20%7B%0A%09if%20%28x%20%3C%3D%201%29%20return%20acc%3B%0A%09else%20return%20fact%28%20x%20-%201%2C%20%28acc%7C%7C1%29*x%20%29%3B%0A%7D%0A%0Avar%20res%20%3D%20fact%28%205%20%29%3B%0A))\n\nHere, `fact` is inferred to have type `(number, number?) => any`, and consequently `res` is inferred to have type `any`.\nThis is vaguely reminiscent of cases #1146, #475, and #523, but those deal with some esoteric circumstances, while this here is a [textbook case](https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.1) of a tail-recursive algorithm. Should work.\n\n_Note_: no, it doesn't have to do with optional parameter, I checked.\n","author":{"url":"https://github.com/fsoikin","@type":"Person","name":"fsoikin"},"datePublished":"2015-06-02T02:28:58Z","url":"https://github.com/microsoft/TypeScript/issues/3336"}
| 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:aee7d32b-c356-f09c-274b-a6f210d6b649 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C716:20AA01:21033A0:29E9C59:6B015D3B |
| html-safe-nonce | 7079639deff2c57138190ee329e10b821c17c0e4ae267d94c5683101c9d214ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzE2OjIwQUEwMToyMTAzM0EwOjI5RTlDNTk6NkIwMTVEM0IiLCJ2aXNpdG9yX2lkIjoiMTU0OTYyMjQwNjg5NTcyMTc4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3ec594a9acb5be0f45068a5d5b2e30352646df9c873f30b5d8b51b5ba54407f5 |
| hovercard-subject-tag | issue:83796698 |
| 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/3336/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c32410ed1d386501aebf11239bf4e370fcd688e540ae9a087506171f8c23c805/microsoft/TypeScript/issues/3336 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c32410ed1d386501aebf11239bf4e370fcd688e540ae9a087506171f8c23c805/microsoft/TypeScript/issues/3336 |
| og:image:alt | Consider this code: function fact( x: number, acc?: number ) { if (x <= 1) return acc; else return fact( x - 1, (acc||1)*x ); } var res = fact( 5 ); (same thing on playground) Here, fact is inferre... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fsoikin |
| 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