Title: Intersection between separate types with getter and setter does not allow assignment to property · Issue #45122 · microsoft/TypeScript · GitHub
Open Graph Title: Intersection between separate types with getter and setter does not allow assignment to property · Issue #45122 · microsoft/TypeScript
X Title: Intersection between separate types with getter and setter does not allow assignment to property · Issue #45122 · microsoft/TypeScript
Description: Bug Report I'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those when the property should be writable. However...
Open Graph Description: Bug Report I'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those when ...
X Description: Bug Report I'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those w...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/45122
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Intersection between separate types with getter and setter does not allow assignment to property","articleBody":"# Bug Report\r\n\r\nI'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those when the property should be writable. However, TypeScript still complains about the property not being writable.\r\n\r\n### 🔎 Search Terms\r\n\r\nintersection getter setter readonly\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nReproducible on the playground with both the latest stable (4.3.5) as well as with 4.4.0 beta and the 'nightly' option.\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about getter / setter / readonly / intersection types\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.3.5#code/C4TwDgpgBAKlC8UDeBzCwpgE4HswAoBKALigGdgsBLAOxQG4BfKAMmTPU1wIDdSLqdQo3oAoAGYBXGgGNgVHDSgBDVfmWkYhZKKgqAdNjwIoAclXLTYxkA)\r\n\r\n[Alternative attempt using `readonly`](https://www.typescriptlang.org/play?ts=4.3.5#code/C4TwDgpgBAKlC8UDeAnCBDAJgewHYBsQowVswAuKAZ2BQEtcBzAbgF8oAyZNLPQ40hWq0GjVswBQAMwCuuAMbA6eKOjUAKdJRgBKZBKiqAdCTIIoAcjXoLk1kA)\r\n\r\n### 💻 Code\r\n\r\nI've tried the following approaches - using a getter and setter:\r\n\r\n```ts\r\ntype T = {get prop(): string;} & {set prop(v: string)};\r\nfunction aaa(a: T) {\r\n a.prop = 'aaa';\r\n}\r\n```\r\n\r\nand also using the `readonly` keyword:\r\n\r\n```ts\r\ntype T = {readonly prop: string;} & {prop: string};\r\nfunction aaa(a: T) {\r\n a.prop = 'aaa';\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nIn both cases the assignment is not allowed by the compiler, and I got a `Cannot assign to 'prop' because it is a read-only property.` error.\r\n\r\n### 🙂 Expected behavior\r\n\r\nI expected `A & B` to behave as \"This type implements both A and B at the same time\". Because of that I would have expected a readonly property defined in A to be writable if B provides a setter.\r\n","author":{"url":"https://github.com/bwrrp","@type":"Person","name":"bwrrp"},"datePublished":"2021-07-20T09:50:59Z","url":"https://github.com/microsoft/TypeScript/issues/45122"}
| 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:1975ab2f-8462-10e8-ee30-1a6fd0633236 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91B6:291B58:20FFD35:29DA19F:6B01AEA4 |
| html-safe-nonce | 07f109d724e18f75d1949c37f91f4bb64e0ccb64d76658f4f8bdb4d3193e4665 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUI2OjI5MUI1ODoyMEZGRDM1OjI5REExOUY6NkIwMUFFQTQiLCJ2aXNpdG9yX2lkIjoiMzg5MjM0MjgwNDE3NjgwOTYzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f5fae3ca9383ff7c428214085654deb7b173d06e9b23da007ac669434cc682b7 |
| hovercard-subject-tag | issue:948488469 |
| 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/45122/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e5e2d4ed24a2447b596f5f699e24d8ebd99dff5c18c60d25ca5afea1235cea7e/microsoft/TypeScript/issues/45122 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e5e2d4ed24a2447b596f5f699e24d8ebd99dff5c18c60d25ca5afea1235cea7e/microsoft/TypeScript/issues/45122 |
| og:image:alt | Bug Report I'm trying to define a property that is writable in some cases and readonly in others, by defining its getter and setter in different types and using the intersection between those when ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bwrrp |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width