Title: Regression in conditional type predicate · Issue #51242 · microsoft/TypeScript · GitHub
Open Graph Title: Regression in conditional type predicate · Issue #51242 · microsoft/TypeScript
X Title: Regression in conditional type predicate · Issue #51242 · microsoft/TypeScript
Description: Bug Report Search Terms: type assertion, type comparison, related to, generic parameter Version & Regression Information: This changed between versions 4.7 and 4.8 Code: Playground Link I discovered this issue in my PR #50454 when I am l...
Open Graph Description: Bug Report Search Terms: type assertion, type comparison, related to, generic parameter Version & Regression Information: This changed between versions 4.7 and 4.8 Code: Playground Link I discovere...
X Description: Bug Report Search Terms: type assertion, type comparison, related to, generic parameter Version & Regression Information: This changed between versions 4.7 and 4.8 Code: Playground Link I disco...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/51242
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Regression in conditional type predicate ","articleBody":"## Bug Report\r\n\r\n**Search Terms:** type assertion, type comparison, related to, generic parameter\r\n\r\n**Version & Regression Information:** This changed between versions 4.7 and 4.8\r\n\r\n**Code:** [Playground Link](https://www.typescriptlang.org/play?ts=4.9.0-dev.20221020#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVIAYAeAFQD4AKWAcwC54SBKem+LAZ3gFEAPDGKJlIAaeHCjA8EAJ7woqaQG0AumQDcAKFCRYCFOmx4kARlKUa9JixjU2nXv0EYRchSvVbw0OEjSZc+IgATGZUNpbMcjZ28ADeAL7wIHwgqMCcJPAA-AzwAGSuSsrw9A4CQiSi4pKoMoXumtreen6GgQDMoRYMkawccYnJGKnpuTmZBfJFJdx85c6V9aqNXrq+BgFIACxd4T3Wtv2ZQyOcU9kDSSlpGRcTSzNlTi7VUrJT7jOoIABuIDArHQ+fT+IyIACsuzo+yih1uJxuhQuCSuw0RmXG+QepTmz0WH1UX1+-0BzXWoMCADYoREDjFjtdRucck8KlUQBI3ksyES-gDPECWhswQB2GkwvrwxlnBQXVkLUQEnn0b580lrEFtJAADnFVlh9NRpzEHJqdQJd15JIFZM1m0QAE49b1okcjYiLZiVcT+U0Na17VBnXT+iiEaMMblJm5ipZ3aNXrV3jHLd61Tb-cLAgAjYMG0ODaVjKPY3LhmXTL3wVXWv3AgNgsB5yVlovMy7l4v3C1xzuJ80pqs1mBW32retZpDAZuuqVopmynJhouR7sp3ttwe5NP-UfqicUpAgGdw1vziuWvumrme7fVn17jMHrWIRAnw2d9sM88PIcPnf8hodpGGAUDsCApjfsaUzmHGAA+DAqJEJAqHEGgAJBwBgyAwIEVCMBcUAzIoUDKJo8RAQ2+CgeBIRQYiaAANaoDgADuqCwbkCEocoyGobEGFYTheFQAROREfQJFkRoFHAdRYEgJ05BUPBiG8ZY-GCSA2G4Ug+GEcRpHkRoQA)\r\n\r\nI discovered this issue in my PR #50454 when I am looking for a possible solution to the typing of `Array.isArray`.\r\nConsider the following code:\r\n\r\n```ts\r\ndeclare function f0\u003cT>(arg: T): arg is Extract\u003cT, readonly any[]>;\r\ndeclare function f1\u003cT>(arg: T): arg is Extract\u003cT, any[]>;\r\ndeclare function f2\u003cT>(arg: T): arg is {} extends T ? T & any[] : Extract\u003cT, readonly any[]>;\r\ndeclare function f3\u003cT>(arg: T): arg is {} extends T ? T & any[] : Extract\u003cT, any[]>;\r\ndeclare function f4\u003cT>(arg: T): arg is T extends any ? {} extends T ? T & any[] : Extract\u003cT, readonly any[]> : never;\r\ndeclare function f5\u003cT>(arg: T): arg is T extends any ? {} extends T ? T & any[] : Extract\u003cT, any[]> : never;\r\ndeclare function f6\u003cT>(arg: T): arg is T extends any ? Extract\u003cT, readonly any[]> : never;\r\ndeclare function f7\u003cT>(arg: T): arg is T extends any ? Extract\u003cT, any[]> : never;\r\ndeclare function f8\u003cT>(arg: T): arg is T extends readonly any[] ? T : never;\r\ndeclare function f9\u003cT>(arg: T): arg is T extends any[] ? T : never;\r\ndeclare function fa\u003cT>(arg: T): arg is {} extends T ? T & any[] : T extends readonly any[] ? T : never;\r\ndeclare function fb\u003cT>(arg: T): arg is {} extends T ? T & any[] : T extends any[] ? T : never;\r\ndeclare function fc\u003cT>(arg: T): arg is T extends any ? {} extends T ? T & any[] : T extends readonly any[] ? T : never : never;\r\ndeclare function fd\u003cT>(arg: T): arg is T extends any ? {} extends T ? T & any[] : T extends any[] ? T : never : never;\r\ndeclare function fe\u003cT>(arg: T): arg is T extends any ? T extends readonly any[] ? T : never : never;\r\ndeclare function ff\u003cT>(arg: T): arg is T extends any ? T extends any[] ? T : never : never;\r\n\r\nfunction case1\u003cT extends any>(a: T | T[]): T[] {\r\n\treturn f(a) ? a : [a];\r\n}\r\nfunction case2\u003cT extends unknown>(a: T | T[]): T[] {\r\n\treturn f(a) ? a : [a];\r\n}\r\nfunction case3\u003cT>(a: T | T[]): T[] {\r\n\treturn f(a) ? a : [a];\r\n}\r\n```\r\n\r\nIn the above, `f4` is the simplified version of the type I used in the PR.\r\n\r\n**Actual behavior:**\r\n\r\nFrom 4.8, the results vary from function to function. In case 3, an error occurs when `f` is `f4` but not in case 1 and case 2 or the case when `f` is `f2` where `T` is not distributed using `T extends any`.\r\nHere is a table of whether a ts2322 error occurs in each cases:\r\n\r\n| f | Case 1 | Case 2 | Case 3 |\r\n|:---:|:---:|:---:|:---:|\r\n| f0 | 🛑 | 🛑 | 🛑 |\r\n| f1 | ⚪ | ⚪ | ⚪ |\r\n| f2 | ⚪ | ⚪ | ⚪ |\r\n| f3 | ⚪ | ⚪ | ⚪ |\r\n| f4 | ⚪ | ⚪ | 🛑 |\r\n| f5 | ⚪ | ⚪ | ⚪ |\r\n| f6 | 🛑 | 🛑 | 🛑 |\r\n| f7 | ⚪ | ⚪ | ⚪ |\r\n\r\nFor the latter half, `f(n+8)` is simply `fn` with `Extract` expanded out, but I could hardly understand why the behavior changes:\r\n\r\n| f | Case 1 | Case 2 | Case 3 |\r\n|:---:|:---:|:---:|:---:|\r\n| f8 | 🛑 | 🛑 | 🛑 |\r\n| f9 | ⚪ | ⚪ | ⚪ |\r\n| fa | ⚪ | ⚪ | ⚪ |\r\n| fb | ⚪ | ⚪ | ⚪ |\r\n| fc | ⚪ | ⚪ | 🛑 |\r\n| fd | ⚪ | ⚪ | 🛑 |\r\n| fe | 🛑 | 🛑 | 🛑 |\r\n| ff | 🛑 | 🛑 | 🛑 |\r\n\r\n(Tested in v4.9.0-dev.20221020 and 4.8.4)\r\n\r\nPrior to 4.8, ALL the cases passed:\r\n\r\n| f | Case 1 | Case 2 | Case 3 |\r\n|:---:|:---:|:---:|:---:|\r\n| f0 | ⚪ | ⚪ | ⚪ |\r\n| f1 | ⚪ | ⚪ | ⚪ |\r\n| f2 | ⚪ | ⚪ | ⚪ |\r\n| f3 | ⚪ | ⚪ | ⚪ |\r\n| f4 | ⚪ | ⚪ | ⚪ |\r\n| f5 | ⚪ | ⚪ | ⚪ |\r\n| f6 | ⚪ | ⚪ | ⚪ |\r\n| f7 | ⚪ | ⚪ | ⚪ |\r\n| f8 | ⚪ | ⚪ | ⚪ |\r\n| f9 | ⚪ | ⚪ | ⚪ |\r\n| fa | ⚪ | ⚪ | ⚪ |\r\n| fb | ⚪ | ⚪ | ⚪ |\r\n| fc | ⚪ | ⚪ | ⚪ |\r\n| fd | ⚪ | ⚪ | ⚪ |\r\n| fe | ⚪ | ⚪ | ⚪ |\r\n| ff | ⚪ | ⚪ | ⚪ |\r\n\r\n(Tested in 4.7.4 and 4.6.4)\r\n\r\n(Note: some of the test functions above are for control (i.e. comparison) only.)\r\n\r\n**Expected behavior:**\r\n\r\nFor each row, the results of case 1, case 2 and case 3 should be identical. (`f4`, `fc`, `fd`)\r\nFor each column, the results of `fn` and `f(n+8)` should be identical. (`f5`, `fd`, `f7`, `ff`)\r\n\r\nI understand that some of them must be desirable changes, so I am not expecting the results to be the same between 4.7 and 4.8, but the mismatched results due to the absent of generic constraint are really weird.","author":{"url":"https://github.com/graphemecluster","@type":"Person","name":"graphemecluster"},"datePublished":"2022-10-20T18:50:22Z","url":"https://github.com/microsoft/TypeScript/issues/51242"}
| 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:fb2e4df8-686f-452f-2bde-01a8f02ebec8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B5B0:16EED3:306C77C:3DB51FE:6B01CE17 |
| html-safe-nonce | abfd4d6bfec4d41907d19a053442cb0fb7b143d2046a5bb55c3535d082d7ca0e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNUIwOjE2RUVEMzozMDZDNzdDOjNEQjUxRkU6NkIwMUNFMTciLCJ2aXNpdG9yX2lkIjoiMjA5NDQ3MzExNDcyODY0ODIxNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b18799eaf557c632f0909a733f5203986950178027fa5afa8ba34b497e9a3daf |
| hovercard-subject-tag | issue:1417099025 |
| 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/51242/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff120a84c4862084c6a88b5e9f916ae2a53fecf0cc53b703017fab7b4d040e55/microsoft/TypeScript/issues/51242 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff120a84c4862084c6a88b5e9f916ae2a53fecf0cc53b703017fab7b4d040e55/microsoft/TypeScript/issues/51242 |
| og:image:alt | Bug Report Search Terms: type assertion, type comparison, related to, generic parameter Version & Regression Information: This changed between versions 4.7 and 4.8 Code: Playground Link I discovere... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | graphemecluster |
| 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