René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-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:5c7e9bfe-74fa-b35d-74ef-00183db6220a
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA2FC:B53AF:C3658F:1052010:6B00F8E9
html-safe-nonced2477eed8135105fa6bd1e9fa95a78b702d9b1fae65357a4c6563bdd04d15d0e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkZDOkI1M0FGOkMzNjU4RjoxMDUyMDEwOjZCMDBGOEU5IiwidmlzaXRvcl9pZCI6Ijc0OTQwNjM0MTQ2NTkwNTM4MDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaca762a5de805ee80d984017877c2e609b5ab9d8140bad2d4acb75bae07397994b
hovercard-subject-tagissue:295234201
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/21729/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4cfe12fb6133640c5eec708a3b17dc658973b4f967d68fa1f3d10f10f8449b91/microsoft/TypeScript/issues/21729
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4cfe12fb6133640c5eec708a3b17dc658973b4f967d68fa1f3d10f10f8449b91/microsoft/TypeScript/issues/21729
og:image:altFurther 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamekrryan
hostnamegithub.com
expected-hostnamegithub.com
Nonee208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187
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
release7dafd017fb7809a839c443dba08622af38e63c09
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/21729#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F21729
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%2F21729
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/21729
Reloadhttps://github.com/microsoft/TypeScript/issues/21729
Reloadhttps://github.com/microsoft/TypeScript/issues/21729
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests309 (309)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/21729
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/21729
#22707https://github.com/microsoft/TypeScript/pull/22707
Bug: Mapped type does not satisfy the constraint it just matched in conditional typehttps://github.com/microsoft/TypeScript/issues/21729#top
#22707https://github.com/microsoft/TypeScript/pull/22707
https://github.com/ahejlsberg
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
TypeScript 2.8.1https://github.com/microsoft/TypeScript/milestone/66
https://github.com/krryan
Kevin Ryan (krryan)https://github.com/krryan
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issue-295234201
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363855434
#21631https://github.com/microsoft/TypeScript/issues/21631
#21709https://github.com/microsoft/TypeScript/pull/21709
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
DuplicateAn existing issue was already createdhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Duplicate%22
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1462942409
https://github.com/krryan
Kevin Ryan (krryan)https://github.com/krryan
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363858363
#21709https://github.com/microsoft/TypeScript/pull/21709
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363862249
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
DuplicateAn existing issue was already createdhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Duplicate%22
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1462983572
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1462983647
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
TypeScript 2.8https://github.com/microsoft/TypeScript/milestone/61
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1462983700
https://github.com/krryan
Kevin Ryan (krryan)https://github.com/krryan
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363863270
Kevin Ryan (krryan)https://github.com/krryan
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1462991721
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363882895
#21709https://github.com/microsoft/TypeScript/pull/21709
https://github.com/krryan
Kevin Ryan (krryan)https://github.com/krryan
on Feb 7, 2018https://github.com/microsoft/TypeScript/issues/21729#issuecomment-363883672
#21709https://github.com/microsoft/TypeScript/pull/21709
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
TypeScript 2.8https://github.com/microsoft/TypeScript/milestone/61
TypeScript 2.9https://github.com/microsoft/TypeScript/milestone/67
on Mar 9, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1514251102
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
on Mar 19, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1529778693
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
TypeScript 2.9https://github.com/microsoft/TypeScript/milestone/67
TypeScript 2.8.1https://github.com/microsoft/TypeScript/milestone/66
on Mar 19, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1529779468
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#22707https://github.com/microsoft/TypeScript/pull/22707
on Mar 21, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1534373910
microsofthttps://github.com/microsoft
on Jul 25, 2018https://github.com/microsoft/TypeScript/issues/21729#event-1753418890
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/21729
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/21729
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
BugA bug in TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Bug%22
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
TypeScript 2.8.1https://github.com/microsoft/TypeScript/milestone/66
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.