René's URL Explorer Experiment


Title: When resolving incompatible signatures, skip callbacks with `never` arguments [ts(2349)] · Issue #42487 · microsoft/TypeScript · GitHub

Open Graph Title: When resolving incompatible signatures, skip callbacks with `never` arguments [ts(2349)] · Issue #42487 · microsoft/TypeScript

X Title: When resolving incompatible signatures, skip callbacks with `never` arguments [ts(2349)] · Issue #42487 · microsoft/TypeScript

Description: Suggestion TypeScript 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: function orDefault(x: T | null,...

Open Graph Description: Suggestion TypeScript 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/metho...

X Description: Suggestion TypeScript 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/m...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/42487

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"When resolving incompatible signatures, skip callbacks with `never` arguments [ts(2349)]","articleBody":"# Suggestion\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: This expression is not callable.\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\nconst yThen = y.map(item => item.length);\r\nconst yChain = orDefault(xs, []).map(item => item.length);\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\nRight now, tsc doesn't distinguish between the two; since they're incompatible, it raises `ts(2349)`. This can make it difficult to write nicely-chainable union values, since the inferred values for \"empty\" or \"impossible\" variants prevent type inference from working.\r\n\r\n\r\nHowever, there's a very easy reason to see why the first overload should be preferred: **values of type `never` shouldn't exist, so a function asking for a `never` will never be called**.\r\n\r\nWhile applying this rule in general is too extreme (since it would likely open up many soundness holes), we can add a small exception to `ts(2349)`: before rejecting the call for having incompatible callback types, check to see if any argument is a callback expecting `never` as any argument. If so, replace that callback with the most-general function type, `(...args: unknown[]) => never`. Then attempt to type-check with this type.\r\n\r\nWe can see that the existing type machinery can handle the code after this \"fix\":\r\n\r\n```ts\r\nconst func: (\u003cU>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[])\r\n    | ((...args: unknown[]) => never)\r\n = null as any;\r\n\r\n// item: string\r\n// res: number[]\r\nconst res = func(item => item.length);\r\n```\r\n\r\nThis change is very narrow, since it only applies to code that:\r\n- is currently rejected by `ts(2349)`\r\n- AND one of the union signature's parameters is a callback \r\n- AND that callback has an argument with type `never`\r\n\r\nIn particular, since it only affects code that today fails to compile, it can't introduce new breaking changes.\r\n\r\nDespite being fairly narrow, it should help out inference for many common functions, especially related to empty arrays and other empty collections, including `.map`, `.filter`, `.flatMap`, `.reduce`, etc.\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\n\r\nList of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.\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\r\n\r\n## 📃 Motivating Example\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// does not type-check today, due to ts(2349)\r\nconst fixed: number[] = orDefault(xs, []).map(item => item.length);\r\n```\r\n\r\n## 💻 Use Cases\r\n\r\nMaking \"chaining\" libraries more ergonomic, especially when dealing with results.\r\n\r\nImproves types for various array methods.\r\n\r\nAlso applies to other collections, like `new Set([])` having type `Set\u003cnever>`; this makes `.forEach` callable on a union value with `new Set([])` as one possible alternative.\r\n\r\n","author":{"url":"https://github.com/Nathan-Fenner","@type":"Person","name":"Nathan-Fenner"},"datePublished":"2021-01-26T00:48:15Z","url":"https://github.com/microsoft/TypeScript/issues/42487"}

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:dffd2334-5c5d-e422-8c15-5c8b41e96e11
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA2F8:14F601:B81FB2:ED60F6:6B00E798
html-safe-nonce69c4fe265aeef749c80f1c2e9454c0c36b809297f727169c5df7b1694e235d37
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkY4OjE0RjYwMTpCODFGQjI6RUQ2MEY2OjZCMDBFNzk4IiwidmlzaXRvcl9pZCI6Ijc0NDEwMjU3NTY4NzM5NDI5MzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac871eb88a7e345c17c921d8ff32b7dab432231212e4901bed079e6d914d49fa0c
hovercard-subject-tagissue:793831282
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/42487/issue_layout
twitter:imagehttps://opengraph.githubassets.com/820bf291ff71d40497671d31f30eebef36c6862a8fd4025107cb433f5358fb6c/microsoft/TypeScript/issues/42487
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/820bf291ff71d40497671d31f30eebef36c6862a8fd4025107cb433f5358fb6c/microsoft/TypeScript/issues/42487
og:image:altSuggestion TypeScript 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/metho...
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/42487#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F42487
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%2F42487
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/42487
Reloadhttps://github.com/microsoft/TypeScript/issues/42487
Reloadhttps://github.com/microsoft/TypeScript/issues/42487
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests308 (308)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/42487
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42487
When resolving incompatible signatures, skip callbacks with never arguments [ts(2349)]https://github.com/microsoft/TypeScript/issues/42487#top
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on Jan 26, 2021https://github.com/microsoft/TypeScript/issues/42487#issue-793831282
#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
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
https://github.com/Nathan-Fenner
Nathan Fenner (Nathan-Fenner)https://github.com/Nathan-Fenner
on Jan 26, 2021https://github.com/microsoft/TypeScript/issues/42487#issuecomment-767225445
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 Jan 26, 2021https://github.com/microsoft/TypeScript/issues/42487#event-4248501089
microsofthttps://github.com/microsoft
on Oct 21, 2025https://github.com/microsoft/TypeScript/issues/42487#event-20417367585
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/42487
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/42487
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.