Title: Consider re-ordering Array#reduce overloads in lib.d.ts · Issue #7014 · microsoft/TypeScript · GitHub
Open Graph Title: Consider re-ordering Array#reduce overloads in lib.d.ts · Issue #7014 · microsoft/TypeScript
X Title: Consider re-ordering Array#reduce overloads in lib.d.ts · Issue #7014 · microsoft/TypeScript
Description: Example code: type UnaryFunction = (arg1) => any; type BinaryFunction = (arg1, arg2) => any; let binaryFuncs: BinaryFunction[] = []; let unaryFunc = arg1 => {}; let reduced = binaryFuncs.reduce((prev, next) => prev, unaryFunc); // ACTUAL...
Open Graph Description: Example code: type UnaryFunction = (arg1) => any; type BinaryFunction = (arg1, arg2) => any; let binaryFuncs: BinaryFunction[] = []; let unaryFunc = arg1 => {}; let reduced = binaryFuncs.reduce((pr...
X Description: Example code: type UnaryFunction = (arg1) => any; type BinaryFunction = (arg1, arg2) => any; let binaryFuncs: BinaryFunction[] = []; let unaryFunc = arg1 => {}; let reduced = binaryFuncs.r...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7014
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Consider re-ordering Array#reduce overloads in lib.d.ts","articleBody":"Example code:\n\n``` typescript\ntype UnaryFunction = (arg1) => any;\ntype BinaryFunction = (arg1, arg2) => any;\n\nlet binaryFuncs: BinaryFunction[] = [];\nlet unaryFunc = arg1 => {};\nlet reduced = binaryFuncs.reduce((prev, next) => prev, unaryFunc);\n\n// ACTUAL:\nlet f: UnaryFunction = reduced; // ERROR binary not assignable to unary\n\n// EXPECTED:\nlet f: UnaryFunction = reduced; // OK - both lhs and rhs really are unary\n```\n\nThe call to `Array#reduce` in the above example definitely returns a unary function, but the type system erroneously infers the return type as a binary function.\n\nThis seems to be caused by the declaration order of the two overloads of `Array#reduce` in `lib.d.ts`. If the declaration order is reversed, the problem is solved.\n\nThe two overloaded declarations in `lib.d.ts` are as follows:\n\n``` typescript\nreduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;\nreduce\u003cU>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;\n```\n\nThe first overload matches the example with `T=BinaryFunction`, since it satisfies the compiler's assignability checks. So the second overload is never considered, even though it is a strictly better match, with `T=BinaryFunction` and `U=UnaryFunction`.\n\nWould it be possible to swap the overload order for `Array#reduce` (and `Array#reduceRight`) in `lib.d.ts` to resolve this issue?\n","author":{"url":"https://github.com/yortus","@type":"Person","name":"yortus"},"datePublished":"2016-02-11T02:35:29Z","url":"https://github.com/microsoft/TypeScript/issues/7014"}
| 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:13d90352-52f9-53f8-0617-f81e31491225 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E600:22DB35:F3FA44:13A1191:6B00FE18 |
| html-safe-nonce | ca2b06c2ba03abd6282cae6f7bb845e9775a3f8fdaef4a8b31e85acf95a8efec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjAwOjIyREIzNTpGM0ZBNDQ6MTNBMTE5MTo2QjAwRkUxOCIsInZpc2l0b3JfaWQiOiIxMDMxOTk1Njg4NTcwOTEyMjgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a8f9bdc22fa1c324a700c632ad0d0eecbb6d678919524d8e480b4df3c54d3ee4 |
| hovercard-subject-tag | issue:132874000 |
| 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/7014/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ab259af9f3e275f4434f7eed7e0e96cb802d81a406a39f5362fb11e5f99972cb/microsoft/TypeScript/issues/7014 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ab259af9f3e275f4434f7eed7e0e96cb802d81a406a39f5362fb11e5f99972cb/microsoft/TypeScript/issues/7014 |
| og:image:alt | Example code: type UnaryFunction = (arg1) => any; type BinaryFunction = (arg1, arg2) => any; let binaryFuncs: BinaryFunction[] = []; let unaryFunc = arg1 => {}; let reduced = binaryFuncs.reduce((pr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yortus |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width