Title: Interface with readonly property is assignable to interface with mutable property · Issue #13347 · microsoft/TypeScript · GitHub
Open Graph Title: Interface with readonly property is assignable to interface with mutable property · Issue #13347 · microsoft/TypeScript
X Title: Interface with readonly property is assignable to interface with mutable property · Issue #13347 · microsoft/TypeScript
Description: TypeScript Version: 2.1.4 Code interface MutableValue
Open Graph Description: TypeScript Version: 2.1.4 Code interface MutableValue
X Description: TypeScript Version: 2.1.4 Code interface MutableValue<T> { value: T; } interface ImmutableValue<T> { readonly value: T; } let i: ImmutableValue<string> = { value: "hi" }...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13347
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Interface with readonly property is assignable to interface with mutable property","articleBody":"**TypeScript Version:** 2.1.4\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface MutableValue\u003cT> {\r\n value: T;\r\n}\r\n\r\ninterface ImmutableValue\u003cT> {\r\n readonly value: T;\r\n}\r\n\r\nlet i: ImmutableValue\u003cstring> = { value: \"hi\" };\r\ni.value = \"Excellent, I can't change it\"; // compile-time error\r\n\r\nlet m: MutableValue\u003cstring> = i;\r\nm.value = \"Oh dear, I can change it\";\r\n```\r\n\r\n**Expected behavior:**\r\nThe assignment of `i` to `m` would fail, to stop us accidentally allowing `value` to be modified.\r\n\r\n**Actual behavior:**\r\nThe assignment is allowed.\r\n\r\n[The current behaviour was a deliberate choice](https://github.com/Microsoft/TypeScript/pull/6532#issuecomment-174356151) so this is a breaking change (or strict flag) feature request rather than a bug report!\r\n\r\nThe Handbook [has this snippet](https://www.typescriptlang.org/docs/handbook/interfaces.html):\r\n\r\n```ts\r\nlet a: number[] = [1, 2, 3, 4];\r\nlet ro: ReadonlyArray\u003cnumber> = a;\r\nro[0] = 12; // error!\r\nro.push(5); // error!\r\nro.length = 100; // error!\r\na = ro; // error!\r\n```\r\n\r\nIt notes that `a = ro` being an error is helpful. But this happens because `ReadonlyArray` has no `push` method, making it incompatible with `Array`.\r\n\r\nMy example above seems \"morally equivalent\" to modelling the input/output flow of values with separate methods:\r\n\r\n```ts\r\ninterface MutableValue\u003cT> {\r\n getValue(): T;\r\n setValue(v: T): void;\r\n}\r\n\r\ninterface ImmutableValue\u003cT> {\r\n getValue(): T;\r\n}\r\n\r\ndeclare let i: ImmutableValue\u003cstring>;\r\ni.setValue(\"Excellent, I can't change it\"); // compile-time error\r\n\r\nlet m: MutableValue\u003cstring> = i;\r\nm.setValue(\"Oh dear, I can change it\");\r\n```\r\n\r\nAnd sure enough, this stops the assignment of `i` to `m`.\r\n\r\nWould be great if mutable and readonly properties had the same relationship as if they were modelled by separate get/set methods (which of course they might actually be, via property getter/setters).","author":{"url":"https://github.com/danielearwicker","@type":"Person","name":"danielearwicker"},"datePublished":"2017-01-07T23:47:19Z","url":"https://github.com/microsoft/TypeScript/issues/13347"}
| 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:3e8410d3-363b-db69-12df-47acba370212 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF38:16E9FD:666F84:87A47F:6AFBC86F |
| html-safe-nonce | a19cfc09f29b537f94672b88a03ef3ec7cd44e996eacb9d08e69a93aa37d9964 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjM4OjE2RTlGRDo2NjZGODQ6ODdBNDdGOjZBRkJDODZGIiwidmlzaXRvcl9pZCI6IjY0MjQ1MTYxMjQ0Mzk0NjQwNDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 37e734c6a4af51dc90955379f78989691db5d683b86def925bd7759382820279 |
| hovercard-subject-tag | issue:199392398 |
| 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/13347/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aa1551d82b5332acc28270d3a077f7340287fb095288ae7e89af1102acb86e53/microsoft/TypeScript/issues/13347 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aa1551d82b5332acc28270d3a077f7340287fb095288ae7e89af1102acb86e53/microsoft/TypeScript/issues/13347 |
| og:image:alt | TypeScript Version: 2.1.4 Code interface MutableValue |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danielearwicker |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| 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 | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width