Title: Is there a special 'hack' for operators and string literal types? · Issue #6505 · microsoft/TypeScript · GitHub
Open Graph Title: Is there a special 'hack' for operators and string literal types? · Issue #6505 · microsoft/TypeScript
X Title: Is there a special 'hack' for operators and string literal types? · Issue #6505 · microsoft/TypeScript
Description: type WeekDay = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; function doFun(a: WeekDay | WeekDay[]) { a = "Monday"; // string literal is contextually typed, so it has string literal type "Monday" => ...
Open Graph Description: type WeekDay = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; function doFun(a: WeekDay | WeekDay[]) { a = "Monday"; // string literal is contextually typed, so...
X Description: type WeekDay = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; function doFun(a: WeekDay | ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/6505
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Is there a special 'hack' for operators and string literal types?","articleBody":"``` typescript\ntype WeekDay = \"Monday\" | \"Tuesday\" | \"Wednesday\" | \"Thursday\" | \"Friday\" | \"Saturday\" | \"Sunday\";\n\nfunction doFun(a: WeekDay | WeekDay[]) {\n a = \"Monday\"; // string literal is contextually typed, so it has string literal type \"Monday\" => no type mismatch\n if (a === \"Monday\") { // string literal is NOT contextually typed and has type 'string' => should be type mismatch error\n\n }\n}\n```\n\nType mismatch error in the second case should be because:\n1) we're comparing 'string' with \"Monday\" | \"Tuesday\" | \"Wednesday\" | \"Thursday\" | \"Friday\" | \"Saturday\" | \"Sunday\" | (\"Monday\" | \"Tuesday\" | \"Wednesday\" | \"Thursday\" | \"Friday\" | \"Saturday\" | \"Sunday\")[] for assignability in both directions\n2) according to the spec, in direction 'string' => the-other-type, we're comparing 'string' with all union type constituents, and it's not assignable to any of them => fail\n3) according to the spec, in direction the-other-type => 'string', all constituents of the-other-type should be assignable to 'string', but obviously Array is not assignable to it => fail\n4) both conditions fail => type mismatch error for '===' operator\n\nBut by some reason the compiler doesn't show any errors here.\n\nIs there some special handling of string literal types with operators like '===', switch labels, etc?\n","author":{"url":"https://github.com/zhuravlikjb","@type":"Person","name":"zhuravlikjb"},"datePublished":"2016-01-15T16:05:39Z","url":"https://github.com/microsoft/TypeScript/issues/6505"}
| 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:753f78eb-d390-ef9c-5c90-0a308bd6ebf8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9ED0:321C40:EB92C2:14505E1:6AFBF578 |
| html-safe-nonce | 73b4aa555c70a0fc121cd58d1f549ac22b9df37343c1dcc4b37b31bb99b1cf39 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RUQwOjMyMUM0MDpFQjkyQzI6MTQ1MDVFMTo2QUZCRjU3OCIsInZpc2l0b3JfaWQiOiIzODM2ODE4NjgyNDQ5NDkxMzIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9cdda6255b0154216cc041302f7836297ebdb776f056fca2984bf277a39bace4 |
| hovercard-subject-tag | issue:126902188 |
| 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/6505/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4a715d51b22dc846b4ea2da04518e5bebfe0f458c2390c9832001fca8e69c184/microsoft/TypeScript/issues/6505 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4a715d51b22dc846b4ea2da04518e5bebfe0f458c2390c9832001fca8e69c184/microsoft/TypeScript/issues/6505 |
| og:image:alt | type WeekDay = "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; function doFun(a: WeekDay | WeekDay[]) { a = "Monday"; // string literal is contextually typed, so... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zhuravlikjb |
| hostname | github.com |
| expected-hostname | github.com |
| None | fa74413a1d84dac536fcca0740780bcbd3207bafec0e51873b73ff8e8cca032f |
| 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 | b3b5633a74535d9c23385512d534c66c8fde1907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width