Title: Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says "both, sometimes" · Issue #42656 · microsoft/TypeScript · GitHub
Open Graph Title: Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says "both, sometimes" · Issue #42656 · microsoft/TypeScript
X Title: Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says "both, sometimes" · Issue #42656 · microsoft/TypeScript
Description: Note: I can't tell if this is a bug in the code or a "bug" in the specification. I figured I'd write my "bug" report first and only follow with a feature request if it turns out this is "working as intended". By "type assignment" I mean ...
Open Graph Description: Note: I can't tell if this is a bug in the code or a "bug" in the specification. I figured I'd write my "bug" report first and only follow with a feature request if it turns out this is "working as...
X Description: Note: I can't tell if this is a bug in the code or a "bug" in the specification. I figured I'd write my "bug" report first and only follow with a feature request if it t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42656
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says \"both, sometimes\"","articleBody":"Note: I can't tell if this is a bug in the code or a \"bug\" in the specification. I figured I'd write my \"bug\" report first and only follow with a feature request if it turns out this is \"working as intended\".\r\n\r\nBy \"type assignment\" I mean setting compiler's expectations about a variable's type using a colon during declaration: `const myThing: MyType = 1`\r\n\r\n# Bug Report\r\nI couldn't find a specification of what type assignment is _supposed_ to do (or even what this syntax is called to begin with)\r\n\r\nTwo plausible-seeming specifications for type assignment, such that behavior is consistent:\r\n\r\n1) \r\n- it enforces structure (blocks compilation if value is incompatible with assigned type)\r\n- it does nothing else (what else is there to do? TS is structurally typed, so as long as there's no problem with the structure, the compiler has no reason to think about this assignment anymore )\r\n\r\n2) \r\n- it enforces structure\r\n- it also _asserts_ structure (overwrites compiler's view of structure)\r\n(this would be a weird spec because if you want this assertion behavior you can just use an assertion)\r\n\r\nIn actuality, the behavior of type assignment is 1) sometimes and 2) other times. So, either consistency across values is not a part of the specification (which would be weirder still) or there's a bug.\r\n\r\n### 🔎 Search Terms\r\n\r\ntype assertion\r\ntype assignment\r\ntype contract\r\ntype declaration\r\ntype check\r\n\r\n### 🕗 Version & Regression Information\r\nI made my gist w/ 4.1.2. AFAIK this discrepancy exists on all previous versions.\r\n\r\nPlease keep and fill in the line that best applies:\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about this\r\n\r\n### ⏯ Playground Link\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?target=99&jsx=0&ts=4.0.5#code/PTAEBUE8AcFMGcDGAnAltALqANrD9RIB7AV1GhK0SIDt4NkBDVG-UW0R+eVAcxoC2sVqAC0o0NSLYAsACgQY0AElQAE1oByLAGsaRAO6gDAC0ZYMJ1AQBmscyWSxQ19jZupEqRtmyRJPrhqnDTBiIw02qAeoS4YAHTyijZEyKCwAB6MAtC4ADSgGLC+LLyFJs7KACIAouXmnJK09BFY8Cak2MEARs6wAG7C8hgwfYM0oAC8oABMoAA+oAAsC6AAbADc8tR0WIxTs1tyO-Sg3QBc6eMHM0cnVJcDwgdLd81Yao-X0wDMG6CKWDIZCpS73JgsLD9VDScwwmjyJJgYhkcITLjwIEWUbwAqWVyuNSodxA4QYACE23e6jmv04BCeNH+igAcgB5dLA0GFZAkZwYrHwxEKMAAVRo2FQOn53EFtAKGL4gjJoAAVGjVeQnJjkIMXDQUsgBHDaEjQAAKbqUdRaLC8PDpDJeIpqACUw1GWtQQhuqx+qwArG9dqAMpdoGgfdNGMHTpBw5HnNNusywBoEKB9FggSDkAruEqhCJwcwRETgln1AgGER-CxDcaMELjtSAF4J71JySpm0ZnOpBXQCNECPeIrC+5nWmzelXYSxrCQGYdqNTnvphlcvP0nVN5o2qv0EF1g2pRvNs0AYkvoBZhhweE0BAY-gMqEsoCcjA0Ev8RG6ABWsCIPgE7UgAggcADemaXHMAC+s73AuoAABorl24HxEyAJpkQGaVv227gAAyqAeiGAQAAGWETJM9GzFRZzAYwJCYnET6fvYP5+GBIYAMKXDBNBfM8iHTMJlz+ohXBNLsPZEQQ1DAsBGB+GIEiWM4JaQi4BCVhEO5ygichUiGABCQmwXOEziaAkkzjJSnvChACaGEHOZ2H-GamRwCBsCfJmRCcrmBRUV5NBMa4o4+hEwQGM45aRHssrIAkZqAOrkgDwf6AOV5bl4EgSQPiXBuoUDjuhYqjpIhVNUh41ieDYmjQ5LtcKig3oAKuSgAAEveaIqIUxTYOU2lEDkqC4GklgNKoBitKA8CTXgVg0GUGAhb0biFJ6VFQSJNnwUxb6WKQHzVsepT6i1e40AA-GZpxVNB1nSUhLmdWA16gIAoOTjTg2TOIt-jmjsGClgQgz0Hw3hjbyrCdstGAkO47otiGiAVIgOgHAAPOAAB85phhArpTET9nwXcOM6PjDmMqA8Ek+Bro9voFXINsdMM9ZTMs+aVTs7hXMFP6riEeM32gL9ANaUDQhpKDFoCKkzgw02vDw5+JBIz69BozYGOTtjwHSmo4FWUdAsHOBHMhURz1UHTgWCfZ-PXHZVQKVuMu-YAMuTlK4Zj6SFiNNj6KCMNAZzWj4K29vAKXGKkePmlR-F0-xBw0E8yBMVabRNr4TRTbg8AYyMcCgFn5uQdM1ewEQNigJBABkHs217RxN7X2cHE3LegK9neMz3-s3vLVgEAYacEPFNk4EQRA6AQkrShAoyiFUaDXFUTzYCORYUvEZ-yGowHYIwTgPnaolMvIZS-L7uaXHPIWS0QWA-GaQdn-EOOHx8LJyiHPZAeMzomDkvQGWyg2iwFgAIdeUpnACH8DHYcjAzbPjMFgFg-RpB6gjsjagl9OC3wVrQdSoNnwhQ0HERIIphpfl8P4Ik8AN7ODmqlHg-AT4wIYKWNgQ1L5HlrLdM8rUmGKHABUdQxI7BOBoIgZwvQMCJWeIqfhNV3gQlYAvWIAp0rwgIO0ToPQJp0GsEUEQ2CQTcFAP0HwfJ4DxCAA)\r\n\r\n### 💻 Code\r\n\r\nI made a comprehensive Gist with expected/actual in comments\r\nhttps://gist.github.com/asktree/8ab244583d4163f09a5a15ba5f60c7a3#file-informationdestruction-ts-L31\r\n\r\nThe gist also covers workarounds.\r\n","author":{"url":"https://github.com/asktree","@type":"Person","name":"asktree"},"datePublished":"2021-02-05T00:27:22Z","url":"https://github.com/microsoft/TypeScript/issues/42656"}
| 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:b17f2a2c-b079-1af2-e961-da3ba68a3dc1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC54:2C69FC:6A987F:8E2A7C:6B01A70B |
| html-safe-nonce | 193764961f86388cfb3b94e08695a47c860f635179da7aef606997a3d18d3d77 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzU0OjJDNjlGQzo2QTk4N0Y6OEUyQTdDOjZCMDFBNzBCIiwidmlzaXRvcl9pZCI6IjMwNTgzMTg3NTE1MzI0MjcwMTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cebaff43fe6b03efa56c77eab9663c392e8e75efbfc798a7e7d997933b27082a |
| hovercard-subject-tag | issue:801738544 |
| 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/42656/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aba26cb4322f215a7eb0a727abebaac5b4e75d090ac2451ac8d27807ffccd24a/microsoft/TypeScript/issues/42656 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aba26cb4322f215a7eb0a727abebaac5b4e75d090ac2451ac8d27807ffccd24a/microsoft/TypeScript/issues/42656 |
| og:image:alt | Note: I can't tell if this is a bug in the code or a "bug" in the specification. I figured I'd write my "bug" report first and only follow with a feature request if it turns out this is "working as... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | asktree |
| 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