Title: Readonly
Open Graph Title: Readonly
X Title: Readonly
Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Code function update(b: Readonly
Open Graph Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Code function update(b: Readonly
X Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Code function update(b: Readonly<Float32Array>) { const c = copy(b); add(c, c); } function add(a: Float32Array, b: Float32Array, c: Float3...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31253
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Readonly\u003cFloat32Array> not assignable to Float32Array","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.0-dev.201xxxxx\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\r\n\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction update(b: Readonly\u003cFloat32Array>) {\r\n const c = copy(b);\r\n add(c, c);\r\n}\r\n\r\nfunction add(a: Float32Array, b: Float32Array, c: Float32Array = a) {\r\n c[0] = a[0] + b[0];\r\n}\r\n\r\nfunction copy(a: Float32Array) {\r\n return new Float32Array(a);\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo error.\r\n\r\n_Though I would expect not to be able to pass a Readonly value to a function which can mutate it but that's [another issue](https://github.com/Microsoft/TypeScript/issues/13347)_.\r\n\r\n**Actual behavior:**\r\n\r\nError on line 2:\r\n\r\n```\r\nArgument of type 'Readonly\u003cFloat32Array>' is not assignable to parameter of type 'Float32Array'.\r\n Type 'Readonly\u003cFloat32Array>' is missing the following properties from type 'Float32Array': [Symbol.iterator], [Symbol.toStringTag]\r\n```\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n\r\n[See in playground](https://www.typescriptlang.org/play/index.html#src=function%20update(b%3A%20Readonly%3CFloat32Array%3E)%20%7B%0D%0A%20%20%20%20const%20c%20%3D%20copy(b)%3B%0D%0A%20%20%20%20add(c%2C%20c)%3B%0D%0A%7D%0D%0A%0D%0Afunction%20add(a%3A%20Float32Array%2C%20b%3A%20Float32Array%2C%20c%3A%20Float32Array%20%3D%20a)%20%7B%0D%0A%20%20%20%20c%5B0%5D%20%3D%20a%5B0%5D%20%2B%20b%5B0%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20copy(a%3A%20Float32Array)%20%7B%0D%0A%20%20%20%20return%20new%20Float32Array(a)%3B%0D%0A%7D)\r\n\r\nNote I can workaround the issue by using mappable types on the function arguments but this is pretty verbose and not something I will want to litter a codebase with - Also not going to work with third-party libraries. See that [here](https://www.typescriptlang.org/play/index.html#src=function%20update(b%3A%20Readonly%3CFloat32Array%3E)%20%7B%0D%0A%20%20%20%20const%20c%20%3D%20copy(b)%3B%0D%0A%20%20%20%20add(c%2C%20c)%3B%0D%0A%7D%0D%0A%0D%0Afunction%20add(a%3A%20Mutable%3CFloat32Array%3E%2C%20b%3A%20Mutable%3CFloat32Array%3E%2C%20c%3A%20Mutable%3CFloat32Array%3E%20%3D%20a)%20%7B%0D%0A%20%20%20%20c%5B0%5D%20%3D%20a%5B0%5D%20%2B%20b%5B0%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20copy(a%3A%20Readonly%3CFloat32Array%3E)%20%7B%0D%0A%20%20%20%20return%20new%20Float32Array(a)%3B%0D%0A%7D%0D%0A%0D%0Atype%20Mutable%3CT%3E%20%3D%20%7B%0D%0A%20%20%20%20-readonly%20%5BP%20in%20keyof%20T%5D%3A%20T%5BP%5D%3B%0D%0A%7D%3B). Edit: Actually seen another way to do it which is much less verbose [here](https://www.typescriptlang.org/play/index.html#src=function%20update(b%3A%20Readonly%3CFloat32Array%3E)%20%7B%0D%0A%20%20%20%20const%20c%20%3D%20copy(b%20as%20Float32Array)%3B%0D%0A%20%20%20%20add(c%2C%20c)%3B%0D%0A%7D%0D%0A%0D%0Afunction%20add(a%3A%20Float32Array%2C%20b%3A%20Float32Array%2C%20c%3A%20Float32Array%20%3D%20a)%20%7B%0D%0A%20%20%20%20c%5B0%5D%20%3D%20a%5B0%5D%20%2B%20b%5B0%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20copy(a%3A%20Float32Array)%20%7B%0D%0A%20%20%20%20return%20new%20Float32Array(a)%3B%0D%0A%7D). _This however loops again back to [this issue](https://github.com/Microsoft/TypeScript/issues/13347) since it really should be raising an error (if in strict mode)._\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n\r\nN/A","author":{"url":"https://github.com/chrisui","@type":"Person","name":"chrisui"},"datePublished":"2019-05-04T11:09:06Z","url":"https://github.com/microsoft/TypeScript/issues/31253"}
| 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:48cc5163-fd58-243c-f865-4c96d070da1c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B9F0:20EE9C:2507BC:349339:6B04696B |
| html-safe-nonce | c41e8cafcd68bd22225886754f0267c5d359c410d0ca2f3d229a566fb601ea14 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOUYwOjIwRUU5QzoyNTA3QkM6MzQ5MzM5OjZCMDQ2OTZCIiwidmlzaXRvcl9pZCI6IjgzNzE1MzY4NTQzOTI3NzQxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fec1767a89ab44b39d0409c212016a36c8402c707333837fb62704540e45f59e |
| hovercard-subject-tag | issue:440311871 |
| 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/31253/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b74c9053fd7352b0680f583bf5aaf14fc12f83168a419f12aaf7cf857535320a/microsoft/TypeScript/issues/31253 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b74c9053fd7352b0680f583bf5aaf14fc12f83168a419f12aaf7cf857535320a/microsoft/TypeScript/issues/31253 |
| og:image:alt | TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Code function update(b: Readonly |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrisui |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| 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 | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width