René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:045bad2c-5a03-d62a-8901-a5b4f9579e62
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBADE:E3C4F:6FCAEE:904E9B:6B00E3F9
html-safe-nonce4700bf6351bae5634ebfd52821e1bbc96a6ed7a47b082ac32d0ca1a111ece026
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQURFOkUzQzRGOjZGQ0FFRTo5MDRFOUI6NkIwMEUzRjkiLCJ2aXNpdG9yX2lkIjoiMjY1OTM5MzY1MDY4NDkxMjYzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac4f5798904765920010cb8ac3463cbac882f09f2ac418f381f3e09153fdacb875
hovercard-subject-tagissue:797538209
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/42558/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3b6fd3000c7dd39d03c0843e1f6acf73740cc856e0352deb2b0586927a83738a/microsoft/TypeScript/issues/42558
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3b6fd3000c7dd39d03c0843e1f6acf73740cc856e0352deb2b0586927a83738a/microsoft/TypeScript/issues/42558
og:image:altSuggestion 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameNathan-Fenner
hostnamegithub.com
expected-hostnamegithub.com
None27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release624179fb8b88d26ba5bba08894eb60a1c5a1d93d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/42558#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F42558
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F42558
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/42558
Reloadhttps://github.com/microsoft/TypeScript/issues/42558
Reloadhttps://github.com/microsoft/TypeScript/issues/42558
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests309 (309)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42558
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42558
Union signatures should exclude "uncallable" callbacks, so methods like .map work on union containing empty arrayhttps://github.com/microsoft/TypeScript/issues/42558#top
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on Jan 30, 2021https://github.com/microsoft/TypeScript/issues/42558#issue-797538209
#40157https://github.com/microsoft/TypeScript/issues/40157
Possibly empty tuple confuses map and reduce methods #38514https://github.com/microsoft/TypeScript/issues/38514
Union of subtypes should be usable as the supertype #38048https://github.com/microsoft/TypeScript/issues/38048
#42487https://github.com/microsoft/TypeScript/issues/42487
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Feb 1, 2021https://github.com/microsoft/TypeScript/issues/42558#event-4275320763
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Feb 1, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-771070417
Wesley Wigham (@weswigham)https://github.com/weswigham
https://github.com/weswigham
Wesley Wigham (weswigham)https://github.com/weswigham
on Feb 1, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-771088364
https://github.com/weswigham
Wesley Wigham (weswigham)https://github.com/weswigham
on Feb 1, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-771095845
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on Feb 1, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-771106044
Wesley Wigham (@weswigham)https://github.com/weswigham
#42559https://github.com/microsoft/TypeScript/pull/42559
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on Feb 17, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-780806483
Wesley Wigham (@weswigham)https://github.com/weswigham
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on May 27, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-849244822
#42620https://github.com/microsoft/TypeScript/pull/42620
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on May 27, 2021https://github.com/microsoft/TypeScript/issues/42558#issuecomment-849281611
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on May 27, 2021https://github.com/microsoft/TypeScript/issues/42558#event-4802990330
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/42558
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42558
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.