Title: String Indexer Assignable to Generic Mapped Type · Issue #28798 · microsoft/TypeScript · GitHub
Open Graph Title: String Indexer Assignable to Generic Mapped Type · Issue #28798 · microsoft/TypeScript
X Title: String Indexer Assignable to Generic Mapped Type · Issue #28798 · microsoft/TypeScript
Description: TypeScript Version: Version 3.3.0-dev Search Terms: string indexer generic mapped type assignable Code This seems like a regression (if it is a bug). I get the expected behaviour on the playground 3.1. function tester
Open Graph Description: TypeScript Version: Version 3.3.0-dev Search Terms: string indexer generic mapped type assignable Code This seems like a regression (if it is a bug). I get the expected behaviour on the playground ...
X Description: TypeScript Version: Version 3.3.0-dev Search Terms: string indexer generic mapped type assignable Code This seems like a regression (if it is a bug). I get the expected behaviour on the playground ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/28798
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"String Indexer Assignable to Generic Mapped Type","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. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** Version 3.3.0-dev\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** string indexer generic mapped type assignable\r\n\r\n**Code**\r\nThis seems like a regression (if it is a bug). I get the expected behaviour on the playground 3.1.\r\n\r\n```ts\r\nfunction tester\u003cK extends 'a'>(x: Record\u003cK, number>, y: { [x: string]: number }) {\r\n x = y; // no error: not expected\r\n y = x; // no error: expected\r\n}\r\n\r\nfunction tester2(x: Record\u003c'a', number>, y: { [x: string]: number }) {\r\n x = y; // error: expected\r\n y = x; // no error: expected\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nA string indexer should not be assignable to a mapped type with a generic constraint.\r\n\r\n**Actual behavior:**\r\n\r\nString indexer appears to be assignable to generic record type.\r\n\r\n**Playground Link:** [playground with expected behaviour](https://www.typescriptlang.org/play/#src=function%20tester%3CK%20extends%20'a'%3E(x%3A%20Record%3CK%2C%20number%3E%2C%20y%3A%20%7B%20%5Bx%3A%20string%5D%3A%20number%20%7D)%20%7B%0D%0A%20%20%20%20x%20%3D%20y%3B%20%2F%2F%20error%3A%20expected%0D%0A%20%20%20%20y%20%3D%20x%3B%20%2F%2F%20no%20error%3A%20expected%0D%0A%7D)\r\n\r\n**Update**. I think this was introduced by #28218.\r\n\r\n```ts\r\n// A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.\r\nif (!isGenericMappedType(source) && isRelatedTo(getConstraintTypeFromMappedType(target), getIndexType(source))) {\r\n const indexedAccessType = getIndexedAccessType(source, getTypeParameterFromMappedType(target));\r\n const templateType = getTemplateTypeFromMappedType(target);\r\n if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {\r\n return result;\r\n }\r\n}\r\n```\r\n\r\nIn this instance we have `'a'` is related `string`, but a type with a key set of type `string` is not guaranteed to have the key `a`. When relating keys I think `string` and `number` should really be bottom types, so a possible fix is to filter them from the source index type:\r\n\r\n```ts\r\nfilterType(getIndexType(source), t => !(t.flags & (TypeFlags.String | TypeFlags.Number)\r\n// and symbol??\r\n```","author":{"url":"https://github.com/jack-williams","@type":"Person","name":"jack-williams"},"datePublished":"2018-12-02T01:32:43Z","url":"https://github.com/microsoft/TypeScript/issues/28798"}
| 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:762e4ccc-abfb-ac36-874a-1042a128ef9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C14A:21AC3F:3960E7F:489E907:6B01D912 |
| html-safe-nonce | 43da51e3cf4aceb18a01b221423158b58e74249cf7750e9b7edc39dad22336b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTRBOjIxQUMzRjozOTYwRTdGOjQ4OUU5MDc6NkIwMUQ5MTIiLCJ2aXNpdG9yX2lkIjoiNDY3MjE1MzkyODQ4MDE4NDU5NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 98fa0fa2ddc1891ef9f74df5592123255dcd13ab9d3cb1aec296517351ecab61 |
| hovercard-subject-tag | issue:386516203 |
| 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/28798/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ed0866c549d96fe97d7781afe7621dd67aba49068259e0ed58f4c87b0d02c6f7/microsoft/TypeScript/issues/28798 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ed0866c549d96fe97d7781afe7621dd67aba49068259e0ed58f4c87b0d02c6f7/microsoft/TypeScript/issues/28798 |
| og:image:alt | TypeScript Version: Version 3.3.0-dev Search Terms: string indexer generic mapped type assignable Code This seems like a regression (if it is a bug). I get the expected behaviour on the playground ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jack-williams |
| 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