Title: Arrow functions treated differently than functions and methods · Issue #29520 · microsoft/TypeScript · GitHub
Open Graph Title: Arrow functions treated differently than functions and methods · Issue #29520 · microsoft/TypeScript
X Title: Arrow functions treated differently than functions and methods · Issue #29520 · microsoft/TypeScript
Description: TypeScript Version: 3.2.2 and 3.3.0-dev.20190119 Search Terms: arrow function method object literal Code type Test
Open Graph Description: TypeScript Version: 3.2.2 and 3.3.0-dev.20190119 Search Terms: arrow function method object literal Code type Test
X Description: TypeScript Version: 3.2.2 and 3.3.0-dev.20190119 Search Terms: arrow function method object literal Code type Test<K extends keyof any> = { [P in K | "foo"]: P extends "foo&quo...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29520
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Arrow functions treated differently than functions and methods","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. --\u003e\r\n**TypeScript Version:** 3.2.2 and 3.3.0-dev.20190119\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) --\u003e\r\n**Search Terms:** arrow function method object literal\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Test\u003cK extends keyof any\u003e = {\r\n [P in K | \"foo\"]: P extends \"foo\" ? true : () =\u003e any\r\n}\r\n\r\nfunction test\u003cT extends Test\u003ckeyof T\u003e\u003e(arg: T) {\r\n return arg;\r\n}\r\n\r\nconst res1 = test({\r\n foo: true,\r\n bar() {\r\n // Notice the type param of `test` is inferred as `Test\u003cnever\u003e`\r\n }\r\n})\r\n\r\nconst res2 = test({\r\n foo: true,\r\n bar: function () {\r\n // Same error here.\r\n }\r\n})\r\n\r\nconst res3 = test({\r\n foo: true,\r\n bar: () =\u003e {\r\n // This is fine.\r\n }\r\n})\r\n```\r\n\r\n**Expected behavior:** The first two `test` calls should infer the given object type correctly.\r\n\r\n**Actual behavior:** They instead infer the object type as `Test\u003cnever\u003e`. 😢\r\n\r\n**Playground Link:** [Click here][1]\r\n\r\n**Related Issues:** #17867 (sort of)\r\n\r\n- https://stackoverflow.com/questions/50198477/how-to-type-check-this-object-without-using-or-any\r\n\r\n[1]: http://www.typescriptlang.org/play/#src=type%20Test%3CK%20extends%20keyof%20any%3E%20%3D%20%7B%0D%0A%20%20%5BP%20in%20K%20%7C%20%22foo%22%5D%3A%20P%20extends%20%22foo%22%20%3F%20true%20%3A%20()%20%3D%3E%20any%0D%0A%7D%0D%0A%0D%0Afunction%20test%3CT%20extends%20Test%3Ckeyof%20T%3E%3E(arg%3A%20T)%20%7B%0D%0A%20%20%20%20return%20arg%3B%0D%0A%7D%0D%0A%0D%0Aconst%20res1%20%3D%20test(%7B%0D%0A%20%20foo%3A%20true%2C%0D%0A%20%20bar()%20%7B%0D%0A%20%20%20%20%2F%2F%20Notice%20the%20type%20param%20of%20%60test%60%20is%20inferred%20as%20%60Test%3Cnever%3E%60%0D%0A%20%20%7D%0D%0A%7D)%0D%0A%0D%0Aconst%20res2%20%3D%20test(%7B%0D%0A%20%20foo%3A%20true%2C%0D%0A%20%20bar%3A%20function%20()%20%7B%0D%0A%20%20%20%20%2F%2F%20Same%20error%20here.%0D%0A%20%20%7D%0D%0A%7D)%0D%0A%0D%0Aconst%20res3%20%3D%20test(%7B%0D%0A%20%20foo%3A%20true%2C%0D%0A%20%20bar%3A%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20%2F%2F%20This%20is%20fine.%0D%0A%20%20%7D%0D%0A%7D)%0D%0A%0D%0Aconst%20res4%20%3D%20test(%7B%0D%0A%20%20foo%3A%20true%2C%0D%0A%20%20bar%2C%0D%0A%7D)%0D%0Afunction%20bar()%20%7B%0D%0A%20%20%2F%2F%20This%20is%20fine.%0D%0A%7D%0D%0A","author":{"url":"https://github.com/aleclarson","@type":"Person","name":"aleclarson"},"datePublished":"2019-01-21T23:07:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/29520/TypeScript/issues/29520"}
| 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:680423ca-bd5b-1e14-9ec9-80e08c526526 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8466:32C291:1FB90A4:2B4A36C:6A630537 |
| html-safe-nonce | fcac9b26d49d4b4ad05d0b9440828cccd869be5147bae087844acbc22b50b7ad |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDY2OjMyQzI5MToxRkI5MEE0OjJCNEEzNkM6NkE2MzA1MzciLCJ2aXNpdG9yX2lkIjoiNTk1MjYzNDQzMjc1OTQ2NTI3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 77c5126b87fa6c8a70b06a040cb6aa0a7cadbce8db5a698583cf336d95eb3d71 |
| hovercard-subject-tag | issue:401539052 |
| 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/29520/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f6d67797f2c9d3ceac8656da9ffe14702a92dc2f4ad1a56ffd95632dab4d9a2/microsoft/TypeScript/issues/29520 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f6d67797f2c9d3ceac8656da9ffe14702a92dc2f4ad1a56ffd95632dab4d9a2/microsoft/TypeScript/issues/29520 |
| og:image:alt | TypeScript Version: 3.2.2 and 3.3.0-dev.20190119 Search Terms: arrow function method object literal Code type Test |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aleclarson |
| hostname | github.com |
| expected-hostname | github.com |
| None | b48e7bf0b1945d51f303503b057abf666ce74c3efbe107b413db2bb921c3f012 |
| 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 | 9370f37042c29822d6ba7e3f670f0036a0155b21 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width