Title: Error with this['prop'] type interacting with || operator · Issue #15376 · microsoft/TypeScript · GitHub
Open Graph Title: Error with this['prop'] type interacting with || operator · Issue #15376 · microsoft/TypeScript
X Title: Error with this['prop'] type interacting with || operator · Issue #15376 · microsoft/TypeScript
Description: TypeScript Version: 2.2.1, 2.3.0 Code // A *self-contained* demonstration of the problem follows... // Compiled with strictNullChecks=true class Foo { test1(foo:string|undefined):string { return foo || 'default'; // no error } test2(para...
Open Graph Description: TypeScript Version: 2.2.1, 2.3.0 Code // A *self-contained* demonstration of the problem follows... // Compiled with strictNullChecks=true class Foo { test1(foo:string|undefined):string { return fo...
X Description: TypeScript Version: 2.2.1, 2.3.0 Code // A *self-contained* demonstration of the problem follows... // Compiled with strictNullChecks=true class Foo { test1(foo:string|undefined):string { return fo...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/15376
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error with this['prop'] type interacting with || operator","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\r\n**TypeScript Version:** 2.2.1, 2.3.0\r\n\r\n**Code**\r\n\r\n```ts\r\n// A *self-contained* demonstration of the problem follows...\r\n// Compiled with strictNullChecks=true\r\nclass Foo {\r\n\ttest1(foo:string|undefined):string {\r\n\t\treturn foo || 'default'; // no error\r\n\t}\r\n\ttest2(params:{foo: string|undefined}):string {\r\n\t\treturn params.foo || 'default'; // no error\r\n\t}\r\n\ttest3(params:Pick\u003c{foo: string|undefined}, 'foo'>):string {\r\n\t\treturn params.foo || 'default'; // no error\r\n\t}\r\n\r\n\tfoo:string|undefined;\r\n\r\n\ttest4(params:Pick\u003cthis, 'foo'>):string {\r\n\t return params.foo || 'default'; // error: undefined not assignable to string\r\n\t}\r\n\ttest5(foo:this['foo']):string {\r\n\t return foo || 'default'; // error: undefined not assignable to string\r\n\t}\r\n\ttest6(foo:this['foo']):string {\r\n\t return (foo as string|undefined) || 'default'; // no error\r\n\t}\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nExpected no errors because the type of the expression `foo || 'default'` should be `string` in all cases. \r\n\r\n**Actual behavior:**\r\nWhen a type is derived indrectly from `this` it seems to break the type inference of the || operator.\r\n\r\n```\r\nError:(17, 6) TS2322:Type 'this[\"foo\"]' is not assignable to type 'string'.\r\n Type 'string | undefined' is not assignable to type 'string'.\r\n Type 'undefined' is not assignable to type 'string'.\r\nError:(20, 6) TS2322:Type 'this[\"foo\"]' is not assignable to type 'string'.\r\n Type 'string | undefined' is not assignable to type 'string'.\r\n Type 'undefined' is not assignable to type 'string'.\r\n```","author":{"url":"https://github.com/adufilie","@type":"Person","name":"adufilie"},"datePublished":"2017-04-25T17:50:55Z","url":"https://github.com/microsoft/TypeScript/issues/15376"}
| 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:4da07611-2879-3830-a822-105c14f8817e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9BB6:317608:CC6E26:104578E:6B00FEE2 |
| html-safe-nonce | 4b530f1659898c9833b5ef257fc16df540398f755226c697bba3a2aa5190ee2a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkI2OjMxNzYwODpDQzZFMjY6MTA0NTc4RTo2QjAwRkVFMiIsInZpc2l0b3JfaWQiOiIxNzYwOTg3OTM1MTk0MjgzNzQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 583bcfa9abcccb74686cb4236d53b08bab154c0a92dcbb71eb7252b9c7913f4c |
| hovercard-subject-tag | issue:224220051 |
| 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/15376/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0b9ca611b9da30e73e0e4677b2bcad69a5aa76397907753b493b64a789cb2073/microsoft/TypeScript/issues/15376 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0b9ca611b9da30e73e0e4677b2bcad69a5aa76397907753b493b64a789cb2073/microsoft/TypeScript/issues/15376 |
| og:image:alt | TypeScript Version: 2.2.1, 2.3.0 Code // A *self-contained* demonstration of the problem follows... // Compiled with strictNullChecks=true class Foo { test1(foo:string|undefined):string { return fo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | adufilie |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width