Title: Bug: Mapped type does not satisfy the constraint it just matched in conditional type · Issue #21729 · microsoft/TypeScript · GitHub
Open Graph Title: Bug: Mapped type does not satisfy the constraint it just matched in conditional type · Issue #21729 · microsoft/TypeScript
X Title: Bug: Mapped type does not satisfy the constraint it just matched in conditional type · Issue #21729 · microsoft/TypeScript
Description: Further testing has demonstrated that generics and type inference are not relevant here; have the same problem without using those. Updated simplified example: interface Foo { foo: string; } interface Bar { bar: string; } type FooBar = F...
Open Graph Description: Further testing has demonstrated that generics and type inference are not relevant here; have the same problem without using those. Updated simplified example: interface Foo { foo: string; } interf...
X Description: Further testing has demonstrated that generics and type inference are not relevant here; have the same problem without using those. Updated simplified example: interface Foo { foo: string; } interf...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/21729
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: Mapped type does not satisfy the constraint it just matched in conditional type","articleBody":"Further testing has demonstrated that generics and type inference are not relevant here; have the same problem without using those.\r\n\r\nUpdated simplified example:\r\n```ts\r\ninterface Foo { foo: string; }\r\ninterface Bar { bar: string; }\r\ntype FooBar = Foo | Bar;\r\ndeclare interface ExtractFooBar\u003cFB extends FooBar> {}\r\n\r\ntype Extracted\u003cStruct> = {\r\n [K in keyof Struct]: Struct[K] extends FooBar ? ExtractFooBar\u003cStruct[K]> : Struct[K];\r\n/* ^^^^^^^^^\r\nType 'Struct[K]' does not satisfy the constraint 'FooBar'.\r\n Type 'Struct[K]' is not assignable to type 'Bar'. */\r\n}\r\n```\r\n\r\nNote that in a non-map situation, this works fine: `T extends FooBar ? ExtractFooBar\u003cT> : T` compiles without a problem. So my guess is that this is related to the type-indexing, whatever stores the constraint on `T` here does not store the same constraint for `Struct[K]`, or the re-use of `Struct[K]` retrieves a \"new\" type through the indexing, and TS doesn't see the relation between the first `Struct[K]` and the second even though `K` has not changed.\r\n\r\nFor more on my use-case and the original problem I had, the original post:\r\n\r\n> Our project uses a concept we call \"fields,\" which are places where data can go from user input, and fields can be set or unset, valid or invalid, etc. And we have a big union of field types representing every combination of those things for different types of fields, and that's called `Field\u003cT>`. The different types of fields encode a lot of behavior, but one of the things we have in there is that, for example, if a field is required, and we have no value for it, then we have an invalid field—period, there is no type in the union that allows required, unset, and valid to exist simultaneously in the same field. That gives us a lot of type-safety.\r\n> \r\n> When it comes time to save a form full of these fields, we want to make sure all fields match the types that are valid. I attempted to use conditional types for this, first to determine what the appropriate type for a given field should be (e.g. given a union of required fields, the savable version is the one that's required, set, and valid), calling that `SavableField\u003cF, T>`. That works fine. Then came the next step: a `SavableForm\u003cForm>` type that maps all properties, and if a property is a field, replacing it with its `SavableField` variant, and otherwise just leaves it as-is. That does not work.\r\n> \r\n> A simplified example:\r\n> \r\n> ```ts\r\n> declare type Field\u003cT>; // Field is a big union of generic types\r\n> declare type SavableField\u003cF extends Field\u003cT>, T>; // big series of type conditions to select \"savable\" types from the union\r\n> \r\n> export type SavableForm\u003cForm> = {\r\n> [K in keyof Form]: Form[K] extends Field\u003cinfer T> ? SavableField\u003cForm[K], T> : Form[K];\r\n> /* ^^^^^^^\r\n> Type 'Form[K]' does not satisfy the constraint 'Field\u003cT>'.\r\n> Type 'Form[K]' is not assignable to type 'ReadOnlyAlwaysSetInvalidForged\u003cT>'. */\r\n> }\r\n> ```\r\n> \r\n> `Form[K]` here has literally just passed the `extends Field\u003cinfer T>` condition, so it failing to be assignable to `Field\u003cT>` is very confusing/surprising here. (For the record, `ReadOnlyAlwaysSetInvalidForged\u003cT>` is the member of the `Field\u003cT>` union that happens to be considered \"last\" by TS, as seen by hovering over the definition of `Field\u003cT>`.)\r\n\r\nNoting that the bug happens only with mapped types allowed a workaround for the above issue, as well:\r\n\r\n```ts\r\ntype SavableIfFieldBounceIfNot\u003cP> = P extends Field\u003cinfer T> ? SavableField\u003cP, T> : P;\r\n\r\nexport type SavableForm\u003cForm> = {\r\n [K in keyof Form]: SavableIfFieldBounceIfNot\u003cForm[K]>;\r\n}\r\n```","author":{"url":"https://github.com/krryan","@type":"Person","name":"krryan"},"datePublished":"2018-02-07T17:55:09Z","url":"https://github.com/microsoft/TypeScript/issues/21729"}
| 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:5c7e9bfe-74fa-b35d-74ef-00183db6220a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A2FC:B53AF:C3658F:1052010:6B00F8E9 |
| html-safe-nonce | d2477eed8135105fa6bd1e9fa95a78b702d9b1fae65357a4c6563bdd04d15d0e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkZDOkI1M0FGOkMzNjU4RjoxMDUyMDEwOjZCMDBGOEU5IiwidmlzaXRvcl9pZCI6Ijc0OTQwNjM0MTQ2NTkwNTM4MDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a762a5de805ee80d984017877c2e609b5ab9d8140bad2d4acb75bae07397994b |
| hovercard-subject-tag | issue:295234201 |
| 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/21729/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4cfe12fb6133640c5eec708a3b17dc658973b4f967d68fa1f3d10f10f8449b91/microsoft/TypeScript/issues/21729 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4cfe12fb6133640c5eec708a3b17dc658973b4f967d68fa1f3d10f10f8449b91/microsoft/TypeScript/issues/21729 |
| og:image:alt | Further testing has demonstrated that generics and type inference are not relevant here; have the same problem without using those. Updated simplified example: interface Foo { foo: string; } interf... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | krryan |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width