Title: Type argument inference for `Extract
Open Graph Title: Type argument inference for `Extract
X Title: Type argument inference for `Extract
Description: TypeScript Version: 2.9.2 Search Terms: keyof generic interference In the example below, the calls to getPropertyX(obj, 'first') should bind the type of K as 'first'. However, when the definition of K involves Extract, the inferred bindi...
Open Graph Description: TypeScript Version: 2.9.2 Search Terms: keyof generic interference In the example below, the calls to getPropertyX(obj, 'first') should bind the type of K as 'first'. However, when the definition o...
X Description: TypeScript Version: 2.9.2 Search Terms: keyof generic interference In the example below, the calls to getPropertyX(obj, 'first') should bind the type of K as 'first'. However, when ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/25065
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type argument inference for `Extract\u003ckey of T, string>` too broad (binds all possible keys)","articleBody":"**TypeScript Version:** 2.9.2\r\n\r\n**Search Terms:** keyof generic interference\r\n\r\nIn the example below, the calls to `getPropertyX(obj, 'first')` should bind the type of K as `'first'`. However, when the definition of K involves `Extract`, the inferred binding is `'first' | 'second'` instead (i.e. all possible keys).\r\n\r\nApropos due to the `keyof` changes in 2.9.\r\n\r\n**Code**\r\n\r\n```ts\r\n// Declaration uses keyof -- no problems\r\nfunction getProperty\u003cT, K extends keyof T>(obj: T, key: K) {\r\n return obj[key];\r\n}\r\n\r\n// Type argument uses bare keyof, function argument uses Extract (doesn't work)\r\nfunction getProperty2\u003cT, K extends keyof T>(obj: T, key: Extract\u003cK, string>): T[K] {\r\n return obj[key];\r\n}\r\n\r\n// Type argument uses Extract (doesn't work)\r\nfunction getProperty3\u003cT, K extends Extract\u003ckeyof T, string>>(obj: T, key: K): T[K] {\r\n return obj[key];\r\n}\r\n\r\n\r\ninterface StrNumPair {\r\n first: string,\r\n second: number,\r\n}\r\nconst obj: StrNumPair = {} as any;\r\n\r\nlet prop: string;\r\n\r\n// Works\r\nprop = getProperty(obj, 'first');\r\n\r\n// Doesn't work -- string | number is not assignable to string\r\nprop = getProperty2(obj, 'first');\r\n\r\n// Doesn't work -- string | number is not assignable to string\r\nprop = getProperty3(obj, 'first');\r\n\r\n// Explicit generic binding -- works\r\nprop = getProperty2\u003cStrNumPair, 'first'>(obj, 'first');\r\nprop = getProperty3\u003cStrNumPair, 'first'>(obj, 'first');\r\n```\r\n\r\n**Expected behavior:** K is bound to just the key passed ('first')\r\n\r\n**Actual behavior:** K is bound to all possible keys ('first' | 'second').\r\n\r\n**Playground Link:** http://www.typescriptlang.org/play/#src=%0D%0Afunction%20getProperty%3CT%2C%20K%20extends%20keyof%20T%3E(obj%3A%20T%2C%20key%3A%20K)%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20getProperty2%3CT%2C%20K%20extends%20keyof%20T%3E(obj%3A%20T%2C%20key%3A%20Extract%3CK%2C%20string%3E)%3A%20T%5BK%5D%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20getProperty3%3CT%2C%20K%20extends%20Extract%3Ckeyof%20T%2C%20string%3E%3E(obj%3A%20T%2C%20key%3A%20K)%3A%20T%5BK%5D%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0A%0D%0Ainterface%20StrNumPair%20%7B%0D%0A%20%20%20%20first%3A%20string%2C%0D%0A%20%20%20%20second%3A%20number%2C%0D%0A%7D%0D%0Aconst%20obj%3A%20StrNumPair%20%3D%20%7B%7D%20as%20any%3B%0D%0A%0D%0Alet%20prop%3A%20string%3B%0D%0A%0D%0A%2F%2F%20Works%0D%0Aprop%20%3D%20getProperty(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Doesn't%20work%20--%20string%20%7C%20number%20is%20not%20assignable%20to%20string%0D%0Aprop%20%3D%20getProperty2(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Doesn't%20work%20--%20string%20%7C%20number%20is%20not%20assignable%20to%20string%0D%0Aprop%20%3D%20getProperty3(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Explicit%20generic%20binding%20--%20works%0D%0Aprop%20%3D%20getProperty2%3CStrNumPair%2C%20'first'%3E(obj%2C%20'first')%3B%0D%0Aprop%20%3D%20getProperty3%3CStrNumPair%2C%20'first'%3E(obj%2C%20'first')%3B\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/7sempra","@type":"Person","name":"7sempra"},"datePublished":"2018-06-19T02:34:35Z","url":"https://github.com/microsoft/TypeScript/issues/25065"}
| 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:edd94935-22db-467c-002b-cf5d4eba6316 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E2DE:19AA62:2164806:2A60400:6B01746A |
| html-safe-nonce | 31c97df8656fac979567caf5bbd5d09624c5d25bdffd5eeb719769992f603f22 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMkRFOjE5QUE2MjoyMTY0ODA2OjJBNjA0MDA6NkIwMTc0NkEiLCJ2aXNpdG9yX2lkIjoiNjM0ODAwNDc5NTM0NDg0Mzg4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a2a87e4f3f04314571f6b1f5335fdf01f03b2d8da7b4c4d6d96540d7c41e21e2 |
| hovercard-subject-tag | issue:333498640 |
| 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/25065/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e8d63782d286e13b9521ba18aad5c701616253b8476dcf35c8d5ca9c241e0947/microsoft/TypeScript/issues/25065 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e8d63782d286e13b9521ba18aad5c701616253b8476dcf35c8d5ca9c241e0947/microsoft/TypeScript/issues/25065 |
| og:image:alt | TypeScript Version: 2.9.2 Search Terms: keyof generic interference In the example below, the calls to getPropertyX(obj, 'first') should bind the type of K as 'first'. However, when the definition o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 7sempra |
| 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