Title: Compile Error when using generic type with `[K in keyof T]?: XXX` · Issue #31070 · microsoft/TypeScript · GitHub
Open Graph Title: Compile Error when using generic type with `[K in keyof T]?: XXX` · Issue #31070 · microsoft/TypeScript
X Title: Compile Error when using generic type with `[K in keyof T]?: XXX` · Issue #31070 · microsoft/TypeScript
Description: TypeScript Version: 3.4.4 Search Terms: keyof index-properties generic-type Code type Numeric
Open Graph Description: TypeScript Version: 3.4.4 Search Terms: keyof index-properties generic-type Code type Numeric
X Description: TypeScript Version: 3.4.4 Search Terms: keyof index-properties generic-type Code type Numeric<T> = { [K in keyof T]?: number } class Entity { aNumber: number; } const fn = <T extends Entit...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31070
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Compile Error when using generic type with `[K in keyof T]?: XXX` ","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:** 3.4.4\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\nkeyof index-properties generic-type\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Numeric\u003cT> = {\r\n [K in keyof T]?: number\r\n}\r\n\r\nclass Entity {\r\n aNumber: number;\r\n}\r\n\r\nconst fn = \u003cT extends Entity>() => {\r\n const n: Numeric\u003cT> = {\r\n// . ^\r\n// . Type '{ aNumber: number; }' is not assignable to type 'Numeric\u003cT>'\r\n aNumber: 1\r\n };\r\n};\r\n```\r\n\r\n**Expected behavior:**\r\nShould not report compile error since in the type constraint of function `fn` was `\u003cT extends Entity>` which means `T` should have `aNumber: number` and `{ aNumber: 1 }` should be satisfied.\r\n\r\n**Actual behavior:**\r\nCompile error said `Type '{ aNumber: number; }' is not assignable to type 'Numeric\u003cT>'`.\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\nhttps://www.typescriptlang.org/play/index.html#src=type%20Numeric%3CT%3E%20%3D%20%7B%0D%0A%20%20%20%20%5BK%20in%20keyof%20T%5D%3F%3A%20number%0D%0A%7D%0D%0A%0D%0Aclass%20Entity%20%7B%0D%0A%20%20%20%20aNumber%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Aconst%20fn%20%3D%20%3CT%20extends%20Entity%3E()%20%3D%3E%20%7B%0D%0A%20%20%20%20const%20n%3A%20Numeric%3CT%3E%20%3D%20%7B%0D%0A%2F%2F%20.%20%20%20%20%20%20%5E%0D%0A%2F%2F%20.%20%20%20%20%20%20Type%20'%7B%20aNumber%3A%20number%3B%20%7D'%20is%20not%20assignable%20to%20type%20'Numeric%3CT%3E'%0D%0A%20%20%20%20%20%20%20%20aNumber%3A%201%0D%0A%20%20%20%20%7D%3B%0D%0A%7D%3B\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\nhttps://stackoverflow.com/questions/55804034/keyof-reported-compile-error-when-using-generic-type","author":{"url":"https://github.com/shaunxu","@type":"Person","name":"shaunxu"},"datePublished":"2019-04-23T06:12:00Z","url":"https://github.com/microsoft/TypeScript/issues/31070"}
| 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:bb1bf5be-454e-ac0a-e866-0bd8e0a2f4d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5A0:2A5566:E5D37E:12E0089:6B01CDD1 |
| html-safe-nonce | 44e9f0e4e0115ae0208459685cd88d3066f708ca9467e393fa47dac03ce8b7d6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUEwOjJBNTU2NjpFNUQzN0U6MTJFMDA4OTo2QjAxQ0REMSIsInZpc2l0b3JfaWQiOiI4MjY3NzE0ODEyODMwNjY2MTk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 816c7e1a15f25b21185de752b124b73ce2f25532f31b399deee5084bfaf77a87 |
| hovercard-subject-tag | issue:436015005 |
| 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/31070/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a40e25889c54799aa3300e10f479bf249192533e45b1b0a632a0f1686e95165/microsoft/TypeScript/issues/31070 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a40e25889c54799aa3300e10f479bf249192533e45b1b0a632a0f1686e95165/microsoft/TypeScript/issues/31070 |
| og:image:alt | TypeScript Version: 3.4.4 Search Terms: keyof index-properties generic-type Code type Numeric |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | shaunxu |
| 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