Title: Generic with type inference in conditional type not evaluated into definitive type · Issue #42636 · microsoft/TypeScript · GitHub
Open Graph Title: Generic with type inference in conditional type not evaluated into definitive type · Issue #42636 · microsoft/TypeScript
X Title: Generic with type inference in conditional type not evaluated into definitive type · Issue #42636 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms generic conditional type inference not evaluated 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic and conditional types ⏯ Playgro...
Open Graph Description: Bug Report 🔎 Search Terms generic conditional type inference not evaluated 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about...
X Description: Bug Report 🔎 Search Terms generic conditional type inference not evaluated 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries a...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42636
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic with type inference in conditional type not evaluated into definitive type","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n--\u003e\r\n\r\n### 🔎 Search Terms\r\n\r\ngeneric conditional type inference not evaluated\r\n\r\n### 🕗 Version \u0026 Regression Information\r\n\r\n\u003c!-- When did you start seeing this bug occur?\r\n\r\n\"Bugs\" that have existed in TS for a long time are very likely to be FAQs; refer to\r\n https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs\r\n\r\nIf possible, please try testing the nightly version of TS to see if it's already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n\r\nNote: The TypeScript Playground can be used to try older versions of TypeScript.\r\n\r\nPlease keep and fill in the line that best applies:\r\n--\u003e\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic and conditional types\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n--\u003e\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.1.3#code/C4TwDgpgBAygrgIwOJwIYCcAmAeAggGigA0oIAPYCAO0wGcoBtXAXQD4oBeYgbgChfQkKAEla8BNgCypCtTpRUVEA2aEYMyjXqLlbTlGnlN8hryhQAdFfEoMOSQypwAtggjpVUAJZUAZu6gAIVZ8M0trUPMrCx0VXmYwgH4gsIAuKCoIADd3PgFwaABRAAZ9UXFsBgBGQgAmQgBmQgAWTwZ6qCaoVtZuKAB6fvNCrNQAGzRKTHTBItKuarrGluYNOXoGaJ9-dCClzpbCaNjmVeTAqHTMnPR+WahCqv1FqA6u1rWtRi2-AMD995HKwnM5BS4ZbK5AZDe6PfRwKgAayoAHsAO5UOK8TAQADGYww0DGEGAUCyxXSJT4OPxhKgxNJWSqlKq1LxBPQRJJZNq6QRyPRmOYeXM5P0TPFtXFrN4gygAD0oAAVApQADk-NRGJUau89FRpNQtFoXgA5lRUAhiVBgCibaq1eVEJUaq9lt02m8Vqw1RYABS1Bq1WoASn4Eq4WSlkeKfTlAHlEbwgA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well--\u003e\r\n```ts\r\ntype SubGuard\u003cA, X extends [A]\u003e = X;\r\n\r\ntype IsSub\u003cM extends any[], S extends any[]\u003e = M extends [\r\n ...SubGuard\u003cM[number], infer B\u003e,\r\n ...S,\r\n ...any[]\r\n]\r\n ? B\r\n : never;\r\n\r\ntype E0 = IsSub\u003c[1, 2, 3, 4], [2, 3, 4]\u003e; // Evaluated: type E0 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never\r\n\r\ntype E1 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never; // type E1 = unknown[]\r\n\r\ndeclare let v0: E0;\r\ndeclare let v1: E1;\r\ndeclare let v2: unknown[];\r\n\r\n v0 = v1 = v2 = v1;\r\n// ^ Type 'unknown[]' is not assignable to type 'IsSub\u003c[1, 2, 3, 4], [2, 3, 4]\u003e'.(2322)\r\n\r\nv1 = v2 = v0; // Ok\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe generic type being provided with correct parameters `IsSub\u003c[1, 2, 3, 4], [2, 3, 4]\u003e` appears to get evaluated into a conditianal type literal `[1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never`\r\n\r\n### 🙂 Expected behavior\r\n\r\nIf such a generic has an error in syntax or faces some limitation in the use, the compiler should raise an exception. If it is correct (it looks correct) it must be evaluated into a definitive type, e.g. `unknown[]`. In contrast, if we change generic:\r\n\r\n```ts\r\ntype IsSub\u003cM extends any[], S extends any[]\u003e = M extends [\r\n ...SubGuard\u003cM[number], infer B\u003e,\r\n ...S\r\n]\r\n ? B\r\n : never;\r\n```\r\nit gives us:\r\n\r\n```ts\r\ntype E0 = IsSub\u003c[1, 2, 3, 4], [2, 3, 4]\u003e; // Evaluated: type E0 = [1]\r\n```\r\nthat is expected.\r\n\r\n---\r\nOne more inconsistency in the example:\r\n\r\nIf we add: \r\n```ts\r\ndeclare let v3: never;\r\n\r\nv3 = v0;\r\n```\r\nthe compiler shows an error:\r\n```\r\nType 'IsSub\u003c[1, 2, 3, 4], [2, 3, 4]\u003e' is not assignable to type 'never'.\r\n Type '[M[number]]' is not assignable to type 'never'.(2322)\r\n```\r\nwhere we can see type `[M[number]]` which is absurd since `M` is not in the context and is the name of a variable in the original generic `type IsSub\u003cM extends any[], S extends any[]\u003e`.\r\n\r\nIf we substitute `M` with `[1, 2, 3, 4]` it still does not answer what actual type we have:\r\n```ts\r\ndeclare let v4: [[1, 2, 3, 4][number]];\r\n\r\nv0 = v4; // Error\r\nv4 = v0; // Ok\r\n```\r\n\r\n[Playground](https://www.typescriptlang.org/play?ts=4.1.3#code/C4TwDgpgBAygrgIwOJwIYCcAmAeAggGigA0oIAPYCAO0wGcoBtXAXQD4oBeYgbgChfQkKAEla8BNgCypCtTpRUVEA2aEYMyjXqLlbTlGnlN8hryhQAdFfEoMOSQypwAtggjpVUAJZUAZu6gAIVZ8M0trUPMrCx0VXmYwgH4gsIAuKCoIADd3PgFwaABRAAZ9UXFsBgBGQgAmQgBmQgAWTwZ6qCaoVtZuKAB6fvNCrNQAGzRKTHTBItKuarrGluYNOXoGaJ9-dCClzpbCaNjmVeTAqHTMnPR+WahCqv1FqA6u1rWtRi2-AMD995HKwnM5BS4ZbK5AZDe6PfRwKgAayoAHsAO5UOK8TAQADGYww0DGEGAUCyxXSJT4OPxhKgxNJWSqlKq1LxBPQRJJZNq6QRyPRmOYeXM5P0TPFtXFrN4gygAD0oAAVApQADk-NRGJUau89FRpNQtFoXgA5lRUAhiVBgCibaq1eVEJUaq9lt02m8Vqw1RYABS1Bq1WoASn4Eq4WSlkeKfTlAHlEfwaRyuYyGldIegRWSGuLY7KhoqVUJHWJnS8vR7CO13T1dV59SjDcazRardBbfbS9d3L7C+ZByXoGqGA4nK53KcG02WybzZbrV37mre+hfQGg6Hk+y6Qyyc10gxK3XmI4XG4PMLw-MD3GhoV0OgUbcss18-eoIneEA)","author":{"url":"https://github.com/turtleflyer","@type":"Person","name":"turtleflyer"},"datePublished":"2021-02-04T04:41:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/42636/TypeScript/issues/42636"}
| 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:c2a6b107-c1b7-5807-ea22-24851db166df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDF8:70D59:191862:21F50F:6A626CB9 |
| html-safe-nonce | e609544f347c83dfdb5ad15b15828ed6699e44d850c980447abf13334d0c3d3a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEREY4OjcwRDU5OjE5MTg2MjoyMUY1MEY6NkE2MjZDQjkiLCJ2aXNpdG9yX2lkIjoiMTA1NzUyMjk4MTMwMzExOTAzMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e61dd7e6c481e48b193e2bf2f49fa5800ab028fe8370d90f2ef99d80e5cd5493 |
| hovercard-subject-tag | issue:800924916 |
| 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/42636/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d86f41c2318af861446cc832238818ba0e87ae466bb96a772e792592e1470062/microsoft/TypeScript/issues/42636 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d86f41c2318af861446cc832238818ba0e87ae466bb96a772e792592e1470062/microsoft/TypeScript/issues/42636 |
| og:image:alt | Bug Report 🔎 Search Terms generic conditional type inference not evaluated 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | turtleflyer |
| hostname | github.com |
| expected-hostname | github.com |
| None | 19769ef9d383a84eeab48bb9b309078c20f4532b73eae2f9713b18406513e0ad |
| 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 | 76dc6b11f13eaeec1329217c9f16c262ade27a1e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width