Title: Template literal index does not transform the key and value types. · Issue #48983 · microsoft/TypeScript · GitHub
Open Graph Title: Template literal index does not transform the key and value types. · Issue #48983 · microsoft/TypeScript
X Title: Template literal index does not transform the key and value types. · Issue #48983 · microsoft/TypeScript
Description: Bug Report I have a data store that uses prefixes to store different types of data. One of the types is "preferences". data = { "preferences.a": string, "preferences.b": number } I have a getter interface for preferences that allows the ...
Open Graph Description: Bug Report I have a data store that uses prefixes to store different types of data. One of the types is "preferences". data = { "preferences.a": string, "preferences.b": number } I have a getter in...
X Description: Bug Report I have a data store that uses prefixes to store different types of data. One of the types is "preferences". data = { "preferences.a": string, "preferences.b"...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48983
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Template literal index does not transform the key and value types.","articleBody":"# Bug Report\r\n\r\nI have a data store that uses prefixes to store different types of data. One of the types is \"preferences\".\r\n\r\n```\r\ndata = {\r\n \"preferences.a\": string,\r\n \"preferences.b\": number\r\n}\r\n```\r\n\r\nI have a getter interface for preferences that allows the user to query without the prefix:\r\n\r\n```\r\ntype PreferencesSchema = {\r\n a: string,\r\n b: number\r\n}\r\nfunction getPreference\u003cKeyT extends keyof PreferencesSchema>(key: KeyT): PreferencesSchema[KeyT] {\r\n return data[`preferences.${key}`];\r\n} \r\n```\r\n\r\nUnfortunately, this gives an error, because TS looks for `a` and `b` on the `data` instead of the actual indexes `preferences.a` and `preferences.b`. I understand template literal types are relatively new, but I think in this case, the compiler should see that the prefixed keys are being used and that the associated values are the same as the value types in the preference schema.\r\n\r\nI found a similar looking issue: https://github.com/microsoft/TypeScript/issues/13948, but I don't think this error can be attributed to type-widening.\r\n\r\n### 🔎 Search Terms\r\n\r\ntemplate literal missing properties\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed between versions 4.3.5 and 4.4.4 (4.3.5 still gives a `noImplicitAny` error, but the introduction of template literal types makes this issue arise even without strict checks)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBACgThAZhBA7AxhAzgZXQCwgFsBDKAXigG8BYAKCihIC4otg4BLVAcwBp6jAEatUAVyJCU9AL7163YCkQlMUACIlgJPIVLVBUAERgEyNJiwA6EkdbsuvAQ2OmkKCBmxWhdqOMlpOjk6enQAe1R2KAATLRYNeN1iMkpaFxMzDy9rW1YjQgAbQvCoAHdwuEKYo2dGTPcLb19WAEYAJgBmABYAVll5OgB6IahgQihOLCwxCHpEMQxgTkioHghgeEbPTAAeAGkIEAAVKAgADyVUGKwoAGsj8MRYLKbcAhSAPgAKB5BWQ4nACUrC25h22GSpAA2oDjgBdAwuBDAMRwVCxeLQgAGbnBOSsABIqH8ZNj4QBuWRQQYjcqVO4kODhRYxeaLdDLVYVOB3bg8ADiGzB2T2cLOl08N3uj2eIreUJIPz+AKOxxBiW0ipxeNF3mJcLJiPSjBRaIxcW0OteEOsxNJ5KpwSAA)\r\n\r\n### 💻 Code\r\n\r\n```\r\ntype PreferencesSchema = {\r\n a: string,\r\n b: number\r\n}\r\n\r\ninterface DataSchema {\r\n \"preferences.a\": string,\r\n \"preferences.b\": number\r\n}\r\n\r\nconst data: DataSchema = {\r\n \"preferences.a\": \"hello world\",\r\n \"preferences.b\": 12345\r\n}\r\n\r\n// the issue\r\nfunction getPreference\u003cKeyT extends keyof PreferencesSchema>(key: KeyT): PreferencesSchema[KeyT] {\r\n return data[`preferences.${key}`];\r\n} \r\n\r\n// workaround\r\nfunction workingGetPreference\u003cKeyT extends keyof PreferencesSchema>(key: KeyT): DataSchema[`preferences.${KeyT}`] {\r\n return data[`preferences.${key}`];\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nAn error occurs:\r\n\r\n```\r\nType 'DataSchema[`preferences.${KeyT}`]' is not assignable to type 'PreferencesSchema[KeyT]'.\r\n Type 'DataSchema' is missing the following properties from type 'PreferencesSchema': a, b\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\nTypeScript should recognize that `preferences.a`/`preferences.b` is being used as an index, not `a`/`b`. Therefore it shouldn't matter that `DataSchema` is missing `a` and `b` as properties.","author":{"url":"https://github.com/benjaminpjones","@type":"Person","name":"benjaminpjones"},"datePublished":"2022-05-06T03:12:53Z","url":"https://github.com/microsoft/TypeScript/issues/48983"}
| 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:bafe3342-5861-56d9-7559-f32f65412455 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C3E0:4BB79:222D5E5:2B9E48F:6B0182E0 |
| html-safe-nonce | bd536358ed7e1ce141a11bb8e48ffe880e25468a0e9372bb7ce0f59224f2bc3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDM0UwOjRCQjc5OjIyMkQ1RTU6MkI5RTQ4Rjo2QjAxODJFMCIsInZpc2l0b3JfaWQiOiIyMjAwNTQ4MTY4MzA1ODM2NzY4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 573367ae8c9e3fe39a1d5e094c30506ba2ff49d0cfdbaf3b3b14462d27846750 |
| hovercard-subject-tag | issue:1227369532 |
| 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/48983/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0c0e3668599d7ebb573c654ccae9f8af06134eb565160d121040cda8ac31b426/microsoft/TypeScript/issues/48983 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0c0e3668599d7ebb573c654ccae9f8af06134eb565160d121040cda8ac31b426/microsoft/TypeScript/issues/48983 |
| og:image:alt | Bug Report I have a data store that uses prefixes to store different types of data. One of the types is "preferences". data = { "preferences.a": string, "preferences.b": number } I have a getter in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | benjaminpjones |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width