Title: Parameter type isn't inferred in values of mapped type with `as`. · Issue #46602 · microsoft/TypeScript · GitHub
Open Graph Title: Parameter type isn't inferred in values of mapped type with `as`. · Issue #46602 · microsoft/TypeScript
X Title: Parameter type isn't inferred in values of mapped type with `as`. · Issue #46602 · microsoft/TypeScript
Description: Bug Report I came across this odd behavior while doing Day 5 of "Type | Treat". 🔎 Search Terms as mapped function parameter infer 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ fo...
Open Graph Description: Bug Report I came across this odd behavior while doing Day 5 of "Type | Treat". 🔎 Search Terms as mapped function parameter infer 🕗 Version & Regression Information This is the behavior in every ve...
X Description: Bug Report I came across this odd behavior while doing Day 5 of "Type | Treat". 🔎 Search Terms as mapped function parameter infer 🕗 Version & Regression Information This is the behavi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/46602
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parameter type isn't inferred in values of mapped type with `as`.","articleBody":"# Bug Report\r\n\r\nI came across this odd behavior while doing Day 5 of \"Type | Treat\".\r\n\r\n### 🔎 Search Terms\r\n\r\nas mapped function parameter infer\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type System Behavior.\r\n\r\nI tried every Playground version with `as` support.\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.4.4#code/GYVwdgxgLglg9mABAIxAcwDwFEDOiCmAHlPmACZ4DkwccliAPopcgIYBOlAfABQD6ALkQBvRAG0siGElyJWeAAYIAJMKwBfBQF0hPfEJxR20tAEpEAXi6IAbnBhlE688IBQ6169RoebxIgQaOH58F3UAGkQAeijEPn05MABPAG4AsCDdBMNjMDNLazsHV38ENnYQsP8YuITWZLSyjiyDIxNzK1t7MndTT1BIWAREAHc4dgBrDjhwMj4ARmw8IhJyKiD6JhYObn4hUQkpJCUwVVxNHUQ9Vtz8zqLHZxF3TzHJ6dmF3xL0oMqRCLRWLxG4mH5NCrxKpA2qgvK9frgaDwJBvKbsGbkPgAJiWBGIpAozA2jGY5V2gkQshWhLwYHwNnw7EQAH4AYh9uJJNIqXh5IgTmcLi1EDl2gUug4nC4Xq40R8sdjvqUMrR-sJATUQaK2vCVeV1epqsDsrq0L0gA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\nfunction bug\u003cEs extends 'foo' | 'bar'>(_: { [E in Es as `on${E}`]: (e: string) => void }) {\r\n}\r\n\r\nbug({\r\n onfoo(_e) {}, // _e: any; onfoo: (e: string) => void\r\n onbar(_e) {} // _e: any; onbar: (e: string) => void\r\n})\r\n\r\nfunction workaround_1\u003cEs extends 'foo' | 'bar'>(_: { [E in `on${Es}`]: (e: string) => void }) {\r\n}\r\n\r\nworkaround_1({\r\n onfoo(_e) {}, // _e: string\r\n onbar(_e) {} // _e: string\r\n})\r\n\r\nfunction workaround_2\u003cEs extends 'foo' | 'bar'>(_: Es extends never ? {} : { [E in Es as `on${E}`]: (e: string) => void }) {\r\n}\r\n\r\nworkaround_2({\r\n onfoo(_e) {}, // _e: string\r\n onbar(_e) {} // _e: string\r\n})\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe types of `onfoo` and `onbar` are correctly inferred as `(e: string) => void` but the parameter `_e` is incorrectly typed as `any` instead of `string`.\r\n\r\n### 🙂 Expected behavior\r\n\r\nI would expect `_e` to have type `string`, because it's known that the function has type `(e: string) => void`. I provided two variations that are typed correctly -- one without using `as`, and one forcing the type to be conditional on `Es`. All three should be equivalent. I'm especially surprised that `bug` and `workaround_2` behave differently since it's (presumably) just forcing `Es` to be evaluated slightly earlier.","author":{"url":"https://github.com/strout","@type":"Person","name":"strout"},"datePublished":"2021-10-30T05:36:59Z","url":"https://github.com/microsoft/TypeScript/issues/46602"}
| 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:11c491e3-0fba-9f1a-2f96-f348c691bb90 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EABE:361639:212585A:2C3DED1:6B018A81 |
| html-safe-nonce | ed05e5dbd0011a10cdadabf634c1508ad972569a9764e34234434315480ee8bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQUJFOjM2MTYzOToyMTI1ODVBOjJDM0RFRDE6NkIwMThBODEiLCJ2aXNpdG9yX2lkIjoiOTIxMTczNDA5NDYxNzA4NjU5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0c410f8d90c96003cd3946d804c82d481ae27bc1a334db907c5406e0ce6930e0 |
| hovercard-subject-tag | issue:1040058204 |
| 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/46602/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3fe8423da42778602f733d0b9944eeca2367f21ec9f094df1e94987f7fdc7b6c/microsoft/TypeScript/issues/46602 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3fe8423da42778602f733d0b9944eeca2367f21ec9f094df1e94987f7fdc7b6c/microsoft/TypeScript/issues/46602 |
| og:image:alt | Bug Report I came across this odd behavior while doing Day 5 of "Type | Treat". 🔎 Search Terms as mapped function parameter infer 🕗 Version & Regression Information This is the behavior in every ve... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | strout |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width