Title: Union signatures should exclude "uncallable" callbacks, so methods like `.map` work on union containing empty array · Issue #42558 · microsoft/TypeScript · GitHub
Open Graph Title: Union signatures should exclude "uncallable" callbacks, so methods like `.map` work on union containing empty array · Issue #42558 · microsoft/TypeScript
X Title: Union signatures should exclude "uncallable" callbacks, so methods like `.map` work on union containing empty array · Issue #42558 · microsoft/TypeScript
Description: Suggestion When merging union signatures, skip "uncallable" callbacks: those that request a never callback argument. These functions can't be called, so they can be removed from consideration when building the function corresponding to a...
Open Graph Description: Suggestion When merging union signatures, skip "uncallable" callbacks: those that request a never callback argument. These functions can't be called, so they can be removed from consideration when ...
X Description: Suggestion When merging union signatures, skip "uncallable" callbacks: those that request a never callback argument. These functions can't be called, so they can be removed from consi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42558
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Union signatures should exclude \"uncallable\" callbacks, so methods like `.map` work on union containing empty array","articleBody":"# Suggestion\r\n\r\nWhen merging union signatures, skip \"uncallable\" callbacks: those that request a `never` callback argument. These functions can't be called, so they can be removed from consideration when building the function corresponding to a union signature.\r\n\r\n## 💻 Use Cases\r\n\r\n\r\nTypeScript sometimes infers empty arrays as having type `never[]`. Usually, this doesn't cause problems, until it gets unioned with another type, and you try to call it as a function/method:\r\n\r\n```ts\r\nfunction orDefault\u003cT, D>(x: T | null, d: D): T | D {\r\n if (x === null) {\r\n return d;\r\n }\r\n return x;\r\n}\r\n\r\nconst xs: string[] | null = [\"a\", \"bc\", \"def\"];\r\n\r\n// y: string[] | never[]\r\nconst y = orDefault(xs, []);\r\n\r\n// ERR: TypeScript 4.1\r\n// Each member of the union type \r\n// (\u003cU>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[])\r\n// | (\u003cU>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])\r\n// has signatures, but none of those signatures are compatible with each other. (2349)\r\n\r\n// ERR: TypeScript 4.2 (dev)\r\n// Parameter 'item' implicitly has an 'any' type.(7006)\r\nconst yThen = y.map(item => item.length);\r\nconst yChain = orDefault(xs, []).map(item => item.length);\r\n```\r\n\r\n\r\nIn this example, since the `[]` is typed as `never[]`, the `.map` method has a union type with two incompatible types; one possibility is a `string`, `number`, `string[]` argument list returning a generic `U`, and the other is a `never`, `number`, `never[]` argument list returning a generic `U`.\r\n\r\nIn TypeScript 4.1, the two signatures were considered \"incompatible\", so attempting to call the function would raise an error. TypeScript 4.2 improves this situation slightly, since the function is now callable; however, it fails to infer the right types for the callback parameters, and so they get defaulted to `any`.\r\n\r\nHowever, there's a generic rule which can be used to \"fix\" the union signature so that the correct typing machinery is activated! Since \"legal\" code can't create values of type `never`, legal code *can't call functions with any argument having type `never`*. For brevity, a callback where any of its required, non-rest parameters have type `never` is called \"uncallable\".\r\n\r\nTo avoid introducing too many new bugs or soundness holes, we can restrict this reasoning just to where union signatures are created. Specifically, when merging two callback types, if one of them is \"uncallable\" and the other is not, then we use the \"callable\" one for their intersection.\r\n\r\n---\r\n\r\nTypeScript currently handles a more-restrictive form of this check. It's able to see that (for example)\r\n\r\n```ts\r\n\r\nconst mapNums: \u003cR>(func: (item: number) => R) => R[]\r\n = null as any;\r\nconst mapNever: \u003cS>(func: (item: never) => S) => S[]\r\n = null as any;\r\n// mapSomething inferred: \u003cR>(func: (item: number) => R) => R[]\r\nconst mapSomething = Math.random() \u003c 1/2 ? mapNever : mapNums;\r\n```\r\n\r\nin this case, `mapSomething` has the same type as `mapNums`, since it can verify that the types are \"compatible\". The problem happens when an \"irrelevant\" type appears, which often occurs for \"methods\" since they have an implicit or explicit `this` parameter:\r\n\r\n```ts\r\nconst mapNums: \u003cR>(func: (item: number, x: string) => R) => R[]\r\n = null as any;\r\nconst mapNever: \u003cS>(func: (item: never, y: number) => S) => S[]\r\n = null as any;\r\n// mapSomething inferred: const mapSomething: (\u003cS>(func: (item: never, y: number) => S) => S[]) | (\u003cR>(func: (item: number, x: string) => R) => R[])\r\nconst mapSomething = Math.random() \u003c 1/2 ? mapNever : mapNums;\r\n```\r\n\r\nnow, the `x` and `y` parameters prevent tsc from assigning a subtyping relation between the two functions. However, since `mapNever` still can't be called, its type has effectively not changed from the previous example! So it's safe to merge them by treating it as a subtype still. That's what this change proposes, to handle some of the rough-cases where `never` doesn't aggressively \"poison\" the types it's wrapped in.\r\n\r\n\r\n## 🔍 Search Terms\r\n\r\n- empty array map\r\n- \"This expression is not callable\"\r\n- Each member of the union type '(\u003cU>(callbackfn: (value: Item, index: number, array: Item[]) => U, thisArg?: any) => U[]) | (\u003cU>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])' has signatures, but none of those signatures are compatible with each other\r\n- ts(2349)\r\n- never type \r\n- incompatible callbacks\r\n\r\nVaguely related, but broader/different from this proposal: #40157\r\n\r\n- specific issue with empty tuples: #38514\r\n- more elaborate related case for ts(2349) not specific to arrays/`never` #38048\r\n\r\nI previously opened and closed #42487 since I originally didn't know about the change in behavior in 4.2.\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\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, new syntax sugar for JS, 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\r\n","author":{"url":"https://github.com/Nathan-Fenner","@type":"Person","name":"Nathan-Fenner"},"datePublished":"2021-01-30T21:18:05Z","url":"https://github.com/microsoft/TypeScript/issues/42558"}
| 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:045bad2c-5a03-d62a-8901-a5b4f9579e62 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BADE:E3C4F:6FCAEE:904E9B:6B00E3F9 |
| html-safe-nonce | 4700bf6351bae5634ebfd52821e1bbc96a6ed7a47b082ac32d0ca1a111ece026 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQURFOkUzQzRGOjZGQ0FFRTo5MDRFOUI6NkIwMEUzRjkiLCJ2aXNpdG9yX2lkIjoiMjY1OTM5MzY1MDY4NDkxMjYzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4f5798904765920010cb8ac3463cbac882f09f2ac418f381f3e09153fdacb875 |
| hovercard-subject-tag | issue:797538209 |
| 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/42558/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b6fd3000c7dd39d03c0843e1f6acf73740cc856e0352deb2b0586927a83738a/microsoft/TypeScript/issues/42558 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b6fd3000c7dd39d03c0843e1f6acf73740cc856e0352deb2b0586927a83738a/microsoft/TypeScript/issues/42558 |
| og:image:alt | Suggestion When merging union signatures, skip "uncallable" callbacks: those that request a never callback argument. These functions can't be called, so they can be removed from consideration when ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Nathan-Fenner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width