Title: `const` Type Parameters fail when mapped · Issue #54537 · microsoft/TypeScript · GitHub
Open Graph Title: `const` Type Parameters fail when mapped · Issue #54537 · microsoft/TypeScript
X Title: `const` Type Parameters fail when mapped · Issue #54537 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms const Type Parameters, mapped types, self types 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about const Type Parameters (but there's nothi...
Open Graph Description: Bug Report 🔎 Search Terms const Type Parameters, mapped types, self types 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about ...
X Description: Bug Report 🔎 Search Terms const Type Parameters, mapped types, self types 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries ab...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/54537
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`const` Type Parameters fail when mapped","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n### 🔎 Search Terms\r\n\r\n`const` Type Parameters, mapped types, self types\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about **`const` Type Parameters** (but there's nothing there about them yet)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBA6glsAFgZQMaIgWwIYB5kQA2AZgHxQC8UA3gFBRQD0jUAKonAM5QQB2nAVwBOEbgRJQuk3sQhCRAEyjAA9lABG0JND7A4IqCvUArCKmDKO3UJEndsYMITgQF9Ji1UAaKAHcOhFoYUCKChHq8AObK4NCB2ABuopbQAFQJ2IQCoqmGxJYqnNAA1hAg9pzuzFAABgK8xbwqvrw1AHTuANoA0tJQpSAq+eLEALoA-ABcUL0QAB7AfArcvBBJQlDjUCM9o1DT9Y3NvADctAC+UABkUAAUtwBEnOhY2A88C0vcA0PbRPlbHZPF44B57abUc4ASg+i14y2ksg2aAwOHcWzoDCxUB6fR+wxB2Am0xRr12sK+UCewCEcCiD3c2IYW04NLpkUZTJJhPJ8zhCIevAEmE0QgZTKxWyFIrknOx01W6zO2PO7gVazkULOtAUZkI2AMxHq5jgKl4UAUKmQKkwECQ7NwqDNrL+JFItxU03gSFJOHw-1IUOmCRUcAU2uqluttvtUQA5GJ-nZeHGLNgBKocHpUJlCCBathuE7+MAaj5dZwwAhoLrUPraVFasWXSN2jqrTa7RwordMVBnqjsBDObxsLbptSG5EHl5OdhIhAJ9LRTP3OdZwxR+OqQBBZyoCCrhjzxdQADMAFZZ9CIywAKJzJxwVAIPNQAScdkFovO0t+FRCMUnAdFGnaxpEvbuAOrzDtiW6npO7JHliJ5LsKK4blA67uPBE57s+h6Yah55XhcUCFlAzbAFqtBAA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype WithSchema\u003cSelf> = {\r\n // This ensures Self is inferred to be the entire object this type is applied\r\n // to, while the resulting type leaves the *values* of those keys as\r\n // `unknown`.\r\n [K in keyof Self]?: K extends never ? Self[K] : unknown;\r\n} & ((\"schema\" extends keyof Self ? Self[\"schema\"] : {}) extends infer Schema\r\n ? {\r\n [K in keyof Schema]?: Schema[K] extends \"string\"\r\n ? string\r\n : Schema[K] extends \"number\"\r\n ? number\r\n : never;\r\n }\r\n : never);\r\n\r\ndeclare function doSomething\u003cconst Self>(o: WithSchema\u003cSelf>): void;\r\n\r\n// doSomething's Self isn't automatically `as const`, despite declaring `const Self`.\r\ndoSomething({\r\n schema: {\r\n name: \"string\",\r\n age: \"number\",\r\n },\r\n name: \"Alice\",\r\n age: 35,\r\n});\r\n\r\n// Explicitly using `as const` works.\r\ndoSomething({\r\n schema: {\r\n name: \"string\",\r\n age: \"number\",\r\n },\r\n name: \"Alice\",\r\n age: 35,\r\n} as const);\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\n`doSomething`'s `Self` parameter was not inferred as `const`.\r\n\r\n### 🙂 Expected behavior\r\n\r\n`doSomething`'s `Self` parameter should be inferred as `const`, since it's marked as `const` and inferred from a literal at the call site.\r\n\r\n### More context\r\n\r\nI'm actually working on something more complex than this: typing [JSON-LD documents](https://www.w3.org/TR/json-ld11/). The core of the issue, though, is that the type of some keys of the object depends on another key (here, `schema`). I'm doing this using a type parameter which infers as the type of the given object. This uses the same technique described in #52088 (which proposes a more native version using a new keyword).\r\n\r\nCurrently, the only way to make these types useful is to ask the user of these types to use `as const` everywhere. Of course, this is less than ideal, which is exactly why [`const` Type Parameters](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters) were introduced. But it appears the more complex usage I have breaks that feature, reverting to inferring a widened type.","author":{"url":"https://github.com/Peeja","@type":"Person","name":"Peeja"},"datePublished":"2023-06-05T17:52:02Z","url":"https://github.com/microsoft/TypeScript/issues/54537"}
| 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:576181b0-6685-2600-0364-653e2dc10905 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C754:2C3566:D9143A:12017E0:6B00F92E |
| html-safe-nonce | bd0dcb7969e1db9bb423b31e462c4913a0e83cda05606fe760c21184db692e91 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzU0OjJDMzU2NjpEOTE0M0E6MTIwMTdFMDo2QjAwRjkyRSIsInZpc2l0b3JfaWQiOiI1MjY2Mzg2Mjg0NzkxNzg5ODcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4812374e15950bdedc798be3d2bb6be695892988858d60452f91d5f1ace65430 |
| hovercard-subject-tag | issue:1742253822 |
| 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/54537/issue_layout |
| twitter:image | https://opengraph.githubassets.com/44883fd293f89118ea1ee23b6696591ca2f4230869e9f26a4e562191268f0af7/microsoft/TypeScript/issues/54537 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/44883fd293f89118ea1ee23b6696591ca2f4230869e9f26a4e562191268f0af7/microsoft/TypeScript/issues/54537 |
| og:image:alt | Bug Report 🔎 Search Terms const Type Parameters, mapped types, self types 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Peeja |
| 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