Title: Incorrect assignability check when using Extract (TS2322) · Issue #46413 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect assignability check when using Extract (TS2322) · Issue #46413 · microsoft/TypeScript
X Title: Incorrect assignability check when using Extract (TS2322) · Issue #46413 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms extract assignable TS2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignability, and Extract. Tested with 4.2.3, 4.3.5...
Open Graph Description: Bug Report 🔎 Search Terms extract assignable TS2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignabilit...
X Description: Bug Report 🔎 Search Terms extract assignable TS2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignab...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/46413
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect assignability check when using Extract (TS2322)","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nextract assignable TS2322\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nThis is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignability, and Extract.\r\n\r\nTested with 4.2.3, 4.3.5, 4.4.4, and 4.5.0-dev.20211018.\r\n\r\nIn 4.1.5, the example using a type alias also fails. Version 4.2 introduced [Smarter Type Alias Preservation](https://devblogs.microsoft.com/typescript/announcing-typescript-4-2-beta/#smarter-type-alias-preservation), which may explain why that example passes starting from 4.2.x.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.4.4#code/C4TwDgpgBAggPAFQHxQLxQN4Cgq6gNwEMAbALigFEAPYAJ0IGNg4A7AVwFsAjCWqAHygBnOgEsWAcwA0UZAG4sAXwWhIsAPrtuvNLFacetJAqwAzNiyaiA9iyimAjAAoq5GJoO8AlG-3bagiK04hIo2HhQAPSRUAAKhEJCEEJQqtAMABYQDADWIQB0OHi0EMBstHZUCopYZhZWtvYATC6+WoZIPnrtOoFikmFFuNFQAGKEosQpaVCZ2XmSUADuosAZULy01nwIAMpNAMxNTaRDUTEI4NAA5PA9RtdQoiks1sBQCUKiEiyEXMTQYDWVJXKC3OBBEICKD3JDXQoREa4S5qa6QxaCe6PZ4wt4fRLfX7-QHAmbXLEIvBIvAom7oiTYl54z6Ev4A1Kk0Hkzy0eFnEplCpQKpKLBAA)\r\n\r\n### 💻 Code\r\n\r\nI have attempted to minimize this example as much as possible. The use of `Extract` to define a field type appears to be significant.\r\n\r\n```ts\r\ntype A\u003cT> = {\r\n val: Extract\u003cnumber | string, T>;\r\n};\r\ntype A_number = A\u003cnumber>;\r\n\r\nfunction f1(x: A_number): A\u003cnumber| string> {\r\n // Passes type checking.\r\n return x;\r\n}\r\n\r\nfunction f2(x: A\u003cnumber>): A\u003cnumber | string> {\r\n // Fails type checking with error TS2322:\r\n // Type 'A\u003cnumber>' is not assignable to type 'A\u003cstring | number>'.\r\n // Type 'string | number' is not assignable to type 'number'.\r\n // Type 'string' is not assignable to type 'number'.\r\n return x;\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe type checker reports error TS2322: `A\u003cnumber>` is not assignable to type `A\u003cstring | number>` because `string | number` is not assignable to `number`. It appears to be checking assignability of a field in the wrong direction, because the assignability check should be whether `number` is assignable to `string | number`.\r\n\r\nUsing a type alias for `A\u003cnumber>` does not produce the same error. I was also not able to reproduce this error without using `Extract` to define a field type.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe type `A\u003cnumber>` should be assignable to type `A\u003cstring | number>` because it is a simple object type and `number` is assignable to `string | number`.","author":{"url":"https://github.com/tonygoold","@type":"Person","name":"tonygoold"},"datePublished":"2021-10-18T16:25:30Z","url":"https://github.com/microsoft/TypeScript/issues/46413"}
| 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:453e0e85-2410-f331-756c-4cd06cb8a503 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9280:D1D28:11D933:17CB02:6B018497 |
| html-safe-nonce | 93759e0fb5a8b515daddcd900afffe938bb08723e253667f70e577e9d8a033c6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjgwOkQxRDI4OjExRDkzMzoxN0NCMDI6NkIwMTg0OTciLCJ2aXNpdG9yX2lkIjoiNTc1NTI4OTAwNDg3MDMwNDkxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ecf0f0ab253a745b7cabd6775af81027aabade0ed652eb4b1f1903d1bb971c45 |
| hovercard-subject-tag | issue:1029337755 |
| 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/46413/issue_layout |
| twitter:image | https://opengraph.githubassets.com/358ecc36dc1fc32edcc9720f13106f16165e91524cdaf87bd5dcd6abb6bbbae1/microsoft/TypeScript/issues/46413 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/358ecc36dc1fc32edcc9720f13106f16165e91524cdaf87bd5dcd6abb6bbbae1/microsoft/TypeScript/issues/46413 |
| og:image:alt | Bug Report 🔎 Search Terms extract assignable TS2322 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics, Assignabilit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonygoold |
| 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