Title: Problem with inference for this['prop'] with conditional types · Issue #27014 · microsoft/TypeScript · GitHub
Open Graph Title: Problem with inference for this['prop'] with conditional types · Issue #27014 · microsoft/TypeScript
X Title: Problem with inference for this['prop'] with conditional types · Issue #27014 · microsoft/TypeScript
Description: TypeScript Version: typescript@3.1.0-dev.20180907 Search Terms: this type, conditional type Code type Wrapped
Open Graph Description: TypeScript Version: typescript@3.1.0-dev.20180907 Search Terms: this type, conditional type Code type Wrapped
X Description: TypeScript Version: typescript@3.1.0-dev.20180907 Search Terms: this type, conditional type Code type Wrapped<T> = { ___secret: T }; type Unwrap<T> = T extends Wrapped<infer U> ? ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27014
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Problem with inference for this['prop'] with conditional types","articleBody":"**TypeScript Version:** typescript@3.1.0-dev.20180907\r\n**Search Terms:** this type, conditional type\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Wrapped\u003cT> = { ___secret: T };\r\ntype Unwrap\u003cT> = T extends Wrapped\u003cinfer U> ? U : T;\r\n\r\ndeclare function set\u003cT, K extends keyof T>(obj: T, key: K, value: Unwrap\u003cT[K]>);\r\n\r\nclass Foo {\r\n prop: Wrapped\u003cstring>;\r\n\r\n method() {\r\n set(this, 'prop', 'hi'); // \u003c-- type error\r\n }\r\n}\r\n\r\nset(new Foo(), 'prop', 'hi'); // \u003c-- typechecks\r\n```\r\n\r\n**Expected behavior:**\r\nBoth calls to `set` (the one in the method with `this` and the external one with `new Foo()`) pass typechecking.\r\n\r\n**Actual behavior:**\r\nThe external call passes, but the one inside `method` fails with `Argument of type '\"hi\"' is not assignable to parameter of type 'Unwrap\u003cthis[\"prop\"]>'.`.\r\n\r\nIf I explicitly annotate `this: Foo` in the method signature or set the type parameters `set\u003cFoo, 'prop'>(...)`, everything passes.\r\n\r\nWe've run into this in the Ember type declarations, and have had to [weaken the types for the time being](https://github.com/typed-ember/ember-cli-typescript/issues/286#) to work around it.\r\n\r\n**Playground Link:** [available here](https://www.typescriptlang.org/play/#src=type%20Wrapped%3CT%3E%20%3D%20%7B%20___secret%3A%20T%20%7D%3B%0D%0Atype%20Unwrap%3CT%3E%20%3D%20T%20extends%20Wrapped%3Cinfer%20U%3E%20%3F%20U%20%3A%20T%3B%0D%0A%0D%0Adeclare%20function%20set%3CT%2C%20K%20extends%20keyof%20T%3E(obj%3A%20T%2C%20key%3A%20K%2C%20value%3A%20Unwrap%3CT%5BK%5D%3E)%3B%0D%0A%0D%0Aclass%20Foo%20%7B%0D%0A%20%20%20%20prop%3A%20Wrapped%3Cstring%3E%3B%0D%0A%0D%0A%20%20%20%20method()%20%7B%0D%0A%20%20%20%20%20%20%20%20set(this%2C%20'prop'%2C%20'hi')%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aset(new%20Foo()%2C%20'prop'%2C%20'hi')%3B)\r\n\r\n**Related Issues:** I wasn't able to find anything that seemed related to this issue.\r\n","author":{"url":"https://github.com/dfreeman","@type":"Person","name":"dfreeman"},"datePublished":"2018-09-10T23:38:00Z","url":"https://github.com/microsoft/TypeScript/issues/27014"}
| 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:d101fdd2-65e0-0a0a-0cb8-a47af9cf3c79 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9946:2463BD:1EE22D2:2772E89:6B01582E |
| html-safe-nonce | 922d53d38347e608d2932d2f865fb1f1fbc26c45a47c581e7e30d1abea151863 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTQ2OjI0NjNCRDoxRUUyMkQyOjI3NzJFODk6NkIwMTU4MkUiLCJ2aXNpdG9yX2lkIjoiNDQ5NDM0ODU3ODg0NzgwNzUzNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 32afad5b9e022d8306d5ba2c4ddebf3ad78e3eef886254890e2c847fc1f543ea |
| hovercard-subject-tag | issue:358832996 |
| 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/27014/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4625dc5a648b3605e808bae6d84092371903d5246ed55594f8f5afd572bd3264/microsoft/TypeScript/issues/27014 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4625dc5a648b3605e808bae6d84092371903d5246ed55594f8f5afd572bd3264/microsoft/TypeScript/issues/27014 |
| og:image:alt | TypeScript Version: typescript@3.1.0-dev.20180907 Search Terms: this type, conditional type Code type Wrapped |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dfreeman |
| 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