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
Domain: github.com
{"@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-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:2526ab8f-870c-9e9a-d0de-d8bdcf9f56cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94FC:A71F9:60822C:78E1AC:6B01129D |
| html-safe-nonce | ea5fd7eebb21778cd7c74ea735b5312c9af38bef02e1065235daceb9f132f9a3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEZDOkE3MUY5OjYwODIyQzo3OEUxQUM6NkIwMTEyOUQiLCJ2aXNpdG9yX2lkIjoiMzk0Nzc5MjUwMjcxMDQwNzgzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d9f97779fbcf1547c73976aad1873c4a1ac7cbf5cc0de73f17445856c768aeec |
| hovercard-subject-tag | issue:343821891 |
| 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/25879/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a5b2b02b033ec1ddf35a287023ee649feaa3940811a39dd9ebc706236d16f62/microsoft/TypeScript/issues/25879 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a5b2b02b033ec1ddf35a287023ee649feaa3940811a39dd9ebc706236d16f62/microsoft/TypeScript/issues/25879 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mattmccutchen |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width