Title: Filter object properties by type · Issue #38646 · microsoft/TypeScript · GitHub
Open Graph Title: Filter object properties by type · Issue #38646 · microsoft/TypeScript
X Title: Filter object properties by type · Issue #38646 · microsoft/TypeScript
Description: TypeScript Version: 3.9.2 Search Terms: mapped type filter object properties never Expected behavior: The type type FilteredKeys
Open Graph Description: TypeScript Version: 3.9.2 Search Terms: mapped type filter object properties never Expected behavior: The type type FilteredKeys
X Description: TypeScript Version: 3.9.2 Search Terms: mapped type filter object properties never Expected behavior: The type type FilteredKeys<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38646
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Filter object properties by type","articleBody":"**TypeScript Version:** 3.9.2\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\nmapped type filter object properties never \r\n\r\n**Expected behavior:**\r\nThe type\r\n`type FilteredKeys\u003cT, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];`\r\nthat allows to extract object properties by type should work everywhere.\r\n\r\n**Actual behavior:**\r\n`type FilteredKeys\u003cT, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];`\r\nonly works externally of the declaring type\r\n\r\n\u003c!-- Did you find other bugs that looked similar? -->\r\n**Related Issues:**\r\n#23199 \r\n\r\n**Code**\r\n```ts\r\ntype FilteredKeys\u003cT, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];\r\n\r\ntype FooItem\u003cT> = T extends number ? { value: T } : undefined;\r\n\r\ntype FooMap\u003cT> = {\r\n // [key in keyof T]: FooItem\u003cT[key]> // \u003c-- This works\r\n [key in FilteredKeys\u003cT, number>]: FooItem\u003cT[key]> // \u003c-- This doesn't work\r\n}\r\n\r\nclass Bar {\r\n a = 1;\r\n b = 2;\r\n c = ''; \r\n\r\n doSomething() {\r\n const fooMap: FooMap\u003cthis> = undefined!; // Omitted...\r\n const a = fooMap.a.value; // \u003c-- No error expected\r\n const c = fooMap.c.value; // \u003c-- Error expected\r\n }\r\n}\r\n\r\nconst fooMap2: FooMap\u003cBar> = undefined!; // Omitted...\r\nconst a = fooMap2.a.value; // \u003c-- No error expected\r\nconst c = fooMap2.c.value; // \u003c-- Error expected\r\n\r\n```\r\n\r\n\r\n\r\n\u003cdetails>\u003csummary>\u003cb>Output\u003c/b>\u003c/summary>\r\n\r\n```ts\r\n\"use strict\";\r\nclass Bar {\r\n constructor() {\r\n this.a = 1;\r\n this.b = 2;\r\n this.c = '';\r\n }\r\n doSomething() {\r\n const fooMap = undefined; // Omitted...\r\n const a = fooMap.a.value; // \u003c-- No error expected\r\n const c = fooMap.c.value; // \u003c-- Error expected\r\n }\r\n}\r\nconst fooMap2 = undefined; // Omitted...\r\nconst a = fooMap2.a.value; // \u003c-- No error expected\r\nconst c = fooMap2.c.value; // \u003c-- Error expected\r\n\r\n```\r\n\r\n\r\n\u003c/details>\r\n\r\n\r\n\u003cdetails>\u003csummary>\u003cb>Compiler Options\u003c/b>\u003c/summary>\r\n\r\n```json\r\n{\r\n \"compilerOptions\": {\r\n \"noImplicitAny\": true,\r\n \"strictNullChecks\": true,\r\n \"strictFunctionTypes\": true,\r\n \"strictPropertyInitialization\": true,\r\n \"strictBindCallApply\": true,\r\n \"noImplicitThis\": true,\r\n \"noImplicitReturns\": true,\r\n \"useDefineForClassFields\": false,\r\n \"alwaysStrict\": true,\r\n \"allowUnreachableCode\": false,\r\n \"allowUnusedLabels\": false,\r\n \"downlevelIteration\": false,\r\n \"noEmitHelpers\": false,\r\n \"noLib\": false,\r\n \"noStrictGenericChecks\": false,\r\n \"noUnusedLocals\": false,\r\n \"noUnusedParameters\": false,\r\n \"esModuleInterop\": true,\r\n \"preserveConstEnums\": false,\r\n \"removeComments\": false,\r\n \"skipLibCheck\": false,\r\n \"checkJs\": false,\r\n \"allowJs\": false,\r\n \"declaration\": true,\r\n \"experimentalDecorators\": false,\r\n \"emitDecoratorMetadata\": false,\r\n \"target\": \"ES2017\",\r\n \"module\": \"ESNext\"\r\n }\r\n}\r\n```\r\n\r\n\r\n\u003c/details>\r\n\r\n**Playground Link:** [Provided](https://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAYglgG2BAThAJgaQiAzgHgBUAaKAVQD4oBeKAbygG0AFKOAOygGscB7AMyiEAugC4hLYVAgAPZO3S5yUAPxRW49hABuqKAF9GPEAKHCA3AFgAUDdCRYvXgElkAWyJVahaXIgKldgBXNwAjPTUGbQBDBCCIcR99KHEghQh+DgwrW2t7aBgnAFlosE8aehsoaqgAelqmYzZOY1MRcUKXdyIjHGEqOob8AFphoQALOCUAd14ULlwqmt6QZthEZDQsHAISKGCw1AoxRy6ID0IV-ur6qBGxwkmldF4IXHYAcmAoWfmbfRsNgAxghorglAAhaIoSrWGpQaIVACMOXhoQqACZUTUgRUPh9zPDAXCai8AMq8NwQYCTdgAcwAFABKWHw+FA3jsXDffjFUodPllGlTLxQNLoDJZdAAQkJtwA8m44MBkOgAHQapZsqAcrnfRG0Xm8EpgNXRNUxOIQOVDUZQAByvGkKBQc18kCBqq1bN13J1FSNJrVQItsXiNrudoAoi63bIPV6SdUAdYU8DOX7A6UMQLjaV8FCUKLxZKtDKI4rlaqNWr03qEQHBRizaGrRH7g6nahXTD4xBPRg637cYam8HW+HBpGxjGe+7+4mgA)\r\n ","author":{"url":"https://github.com/turolla","@type":"Person","name":"turolla"},"datePublished":"2020-05-18T18:49:45Z","url":"https://github.com/microsoft/TypeScript/issues/38646"}
| 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:ced4659d-d559-65b3-be23-d7b2ac53bd33 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC90:154D88:1F6A841:28288CB:6B01AE1F |
| html-safe-nonce | 36776dd51fe6084c1c99683c73e4f0e3a608ffb92c701beab273d21e231d81c7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzkwOjE1NEQ4ODoxRjZBODQxOjI4Mjg4Q0I6NkIwMUFFMUYiLCJ2aXNpdG9yX2lkIjoiODk0MjU3MjAxNjA0OTk1ODQzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 299a25c732d12ab1951549ffb02f7727b9de79a2b8260d792ef016a50ab5a7e8 |
| hovercard-subject-tag | issue:620429211 |
| 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/38646/issue_layout |
| twitter:image | https://opengraph.githubassets.com/023159d86a3e9d165ef0f9f7109fb8927a4fdd8a3dc449d1aec2753491c29785/microsoft/TypeScript/issues/38646 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/023159d86a3e9d165ef0f9f7109fb8927a4fdd8a3dc449d1aec2753491c29785/microsoft/TypeScript/issues/38646 |
| og:image:alt | TypeScript Version: 3.9.2 Search Terms: mapped type filter object properties never Expected behavior: The type type FilteredKeys |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | turolla |
| 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