René's URL Explorer Experiment


Title: Allow type variables to be constrained singleton, causing lookup into a non-generic mapped type to substitute · Issue #25879 · microsoft/TypeScript · GitHub

Open Graph Title: Allow type variables to be constrained singleton, causing lookup into a non-generic mapped type to substitute · Issue #25879 · microsoft/TypeScript

X Title: Allow type variables to be constrained singleton, causing lookup into a non-generic mapped type to substitute · Issue #25879 · microsoft/TypeScript

Description: I feel awkward submitting this suggestion since I don't know if it will get enough votes to go anywhere, but I guess someone has to be the initial submitter for each suggestion... Search Terms mapped type indexed access type lookup type ...

Open Graph Description: I feel awkward submitting this suggestion since I don't know if it will get enough votes to go anywhere, but I guess someone has to be the initial submitter for each suggestion... Search Terms mapp...

X Description: I feel awkward submitting this suggestion since I don't know if it will get enough votes to go anywhere, but I guess someone has to be the initial submitter for each suggestion... Search Terms ...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/25879

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow type variables to be constrained singleton, causing lookup into a non-generic mapped type to substitute","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the \"Common Feature Requests\" section.\r\n\r\n-->\r\n\r\nI feel awkward submitting this suggestion since I don't know if it will get enough votes to go anywhere, but I guess _someone_ has to be the initial submitter for each suggestion...\r\n\r\n## Search Terms\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\nmapped type indexed access type lookup type substitute substitution generic\r\n\r\n## Suggestion\r\n\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\n\r\nCurrently, a lookup into a mapped type, for example `{ [P in K]: Box\u003cT[P]> }[X]`, is simplified by substitution (in this example, to produce `Box\u003cT[X]>`) only if the constraint type `K` is generic; this is unsound but I guess it was useful in some cases.  I'd like to be able to constrain a type parameter `X` to be a singleton type, causing substitution to occur (which is sound) regardless of whether `K` is generic.\r\n\r\n## Use Case\r\n\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n-->\r\nSuppose we have a codebase with an enum `E` and many functions that simulate dependent types by taking a type parameter `A extends E` (where `A` is intended to be a singleton type) along with a value of type `A`.  Given a generic type `T\u003cA extends E>`, we may want an object that contains a `T\u003cA>` for each `A` in `E`, i.e., `{[A in E]: T\u003cA>}`.  Then we'd like to pass this object to a function along with a particular choice of `A` and have it manipulate the corresponding property.  We should get a type error if the function uses the wrong property.  Currently, a lookup type expression like `{[A in E]: T\u003cA>}[A1]` does not substitute (because the constraint type `E` is not generic), so all reads and writes to the property are checked using the constraint of the lookup type, which is `{[A in E]: T\u003cA>}[E]`, and in effect we get no distinction among the properties of the object.\r\n\r\nSpecifically, I'm writing a structured spreadsheet tool that [manipulates row and column IDs](https://bitbucket.org/espalier-spreadsheet/espalier/src/5a582831912434caaf703f6dcc5b9e18d7dfa2c7/src/layout/core.ts?at=master&fileviewer=file-view-default).  A rectangle ID is a pair of a row ID and a column ID.  I wanted to brand the row and column IDs differently to ensure I don't mix them up.  I have many functions that are parameterized over an axis: for example, `getRectParentOnAxis` takes a rectangle and can either find the rectangle that covers the same column and a larger row, or the same row and a larger column.\r\n\r\nOne current approach, which I've taken and I call the \"generic index\" hack, is to add an artificial type variable to every relevant type and function so that I can ensure the constraint type of the mapped type is always generic and the mapped type will always substitute.  (See \"Workaround\" below.)  This is ugly, but I wanted the checking badly enough to do it.\r\n\r\n## Examples\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n```ts\r\nenum Axis {\r\n    ROW = \"row\",\r\n    COL = \"col\",\r\n}\r\nconst AXIS_BRAND = Symbol();\r\ntype SpanId\u003cA extends Axis> = string & {[AXIS_BRAND]: A};\r\n\r\ntype Rectangle = {[A in Axis]: SpanId\u003cA>};\r\n\r\nfunction getRectangleSide\u003cA in Axis>(rect: Rectangle, a: A): SpanId\u003cA> {\r\n    // Error with `A extends axis`: `Rectangle[A]` doesn't simplify and isn't assignable to `SpanId\u003cA>`\r\n    // Allowed with `A in Axis`: `Rectangle[A]` simplifies to `SpanId\u003cA>`\r\n    return rect[a];\r\n}\r\nfunction getRectangleSide2\u003cA in Axis>(rect: Rectangle, a: A): Rectangle[A] {\r\n    if (Math.random() > 0.5) {\r\n        return rect[a];\r\n    } else {\r\n        // Allowed with `A extends axis`: `SpanId\u003cAxis.ROW>` is unsoundly assignable to `Rectangle[A]`\r\n        // because it is assignable to the constraint `SpanId\u003cAxis.ROW> | SpanId\u003cAxis.COL>`\r\n        // Error with `A in Axis`: `SpanId\u003cAxis.ROW>` is not assignable to `SpanId\u003cA>`\r\n        return rect[Axis.ROW];\r\n    }\r\n}\r\n```\r\n\r\n## Workaround\r\n```ts\r\nconst FAKE_INDEX = \"fake-index\";\r\ntype GenericIndex\u003c_, K> = K | (_ & typeof FAKE_INDEX);\r\ntype LooseIndex\u003cK> = K | typeof FAKE_INDEX;\r\n\r\nenum Axis {\r\n    ROW = \"row\",\r\n    COL = \"col\",\r\n}\r\ntype AxisG\u003c_> = GenericIndex\u003c_, Axis>;\r\ntype AxisL = LooseIndex\u003cAxis>;\r\nconst AXIS_BRAND = Symbol();\r\ntype SpanId\u003cA extends AxisL> = string & {[AXIS_BRAND]: A};\r\n\r\ntype Rectangle\u003c_> = {[A in AxisG\u003c_>]: SpanId\u003cA>};\r\nfunction getRectangleSide\u003c_, A extends Axis>(rect: Rectangle\u003c_>, a: A): SpanId\u003cA> {\r\n    return rect[a];  // allowed\r\n}\r\nfunction getRectangleSide2\u003c_, A extends Axis>(rect: Rectangle\u003c_>, a: A): Rectangle\u003c_>[A] {\r\n    if (Math.random() > 0.5) {\r\n        return rect[a];\r\n    } else {\r\n        return rect[Axis.ROW];  // error\r\n    }\r\n}\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [X] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [X] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [X] This could be implemented without emitting different JS based on the types of the expressions\r\n* [X] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n","author":{"url":"https://github.com/mattmccutchen","@type":"Person","name":"mattmccutchen"},"datePublished":"2018-07-23T22:43:30Z","url":"https://github.com/microsoft/TypeScript/issues/25879"}

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:2526ab8f-870c-9e9a-d0de-d8bdcf9f56cc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id94FC:A71F9:60822C:78E1AC:6B01129D
html-safe-nonceea5fd7eebb21778cd7c74ea735b5312c9af38bef02e1065235daceb9f132f9a3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEZDOkE3MUY5OjYwODIyQzo3OEUxQUM6NkIwMTEyOUQiLCJ2aXNpdG9yX2lkIjoiMzk0Nzc5MjUwMjcxMDQwNzgzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacd9f97779fbcf1547c73976aad1873c4a1ac7cbf5cc0de73f17445856c768aeec
hovercard-subject-tagissue:343821891
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/25879/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3a5b2b02b033ec1ddf35a287023ee649feaa3940811a39dd9ebc706236d16f62/microsoft/TypeScript/issues/25879
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3a5b2b02b033ec1ddf35a287023ee649feaa3940811a39dd9ebc706236d16f62/microsoft/TypeScript/issues/25879
og:image:altI feel awkward submitting this suggestion since I don't know if it will get enough votes to go anywhere, but I guess someone has to be the initial submitter for each suggestion... Search Terms mapp...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemattmccutchen
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
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
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/25879#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F25879
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%2F25879
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/25879
Reloadhttps://github.com/microsoft/TypeScript/issues/25879
Reloadhttps://github.com/microsoft/TypeScript/issues/25879
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests310 (310)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/25879
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/25879
Allow type variables to be constrained singleton, causing lookup into a non-generic mapped type to substitutehttps://github.com/microsoft/TypeScript/issues/25879#top
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/mattmccutchen
Matt McCutchen (mattmccutchen)https://github.com/mattmccutchen
on Jul 23, 2018https://github.com/microsoft/TypeScript/issues/25879#issue-343821891
manipulates row and column IDshttps://bitbucket.org/espalier-spreadsheet/espalier/src/5a582831912434caaf703f6dcc5b9e18d7dfa2c7/src/layout/core.ts?at=master&fileviewer=file-view-default
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
on Aug 7, 2018https://github.com/microsoft/TypeScript/issues/25879#event-1776496618
Matt McCutchen (mattmccutchen)https://github.com/mattmccutchen
on Aug 23, 2018https://github.com/microsoft/TypeScript/issues/25879#event-1806425780
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on Jun 6, 2019https://github.com/microsoft/TypeScript/issues/25879#issuecomment-499612412
#31672https://github.com/microsoft/TypeScript/issues/31672
#13995https://github.com/microsoft/TypeScript/issues/13995
https://github.com/alythobani
Aly Thobani (alythobani)https://github.com/alythobani
on May 18, 2024https://github.com/microsoft/TypeScript/issues/25879#issuecomment-2118979924
#13995https://github.com/microsoft/TypeScript/issues/13995
#46899https://github.com/microsoft/TypeScript/issues/46899
#27808https://github.com/microsoft/TypeScript/issues/27808
Matt McCutchen (@mattmccutchen)https://github.com/mattmccutchen
#43183https://github.com/microsoft/TypeScript/pull/43183
#13995https://github.com/microsoft/TypeScript/issues/13995
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/25879
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/25879
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
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.