-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assignment of immutable record into mutable record variable does not produce error #52225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is not specific to |
Hence why I always try to tell people: readonly does not mean immutable. It only means readonly via that interface. |
The behavior is inconsistent: const immutableRecord: Readonly<Record<string, unknown>> = {};
// Doesn't fail
const mutableRecord: Record<string, unknown> = immutableRecord;
const immutableArray: ReadonlyArray<string> = [];
// This breaks, as it should
// The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'
const mutableArray: Array<string> = immutableArray; |
It’s consistent w.r.t. to the structural typing rules: the readonly array is not assignable only as a side effect of missing methods, not directly due to its readonly-ness (despite what the error message says) |
Aka, it is inconsistent. From the user's perspective, it doesn't really matter if the part that works as expected (the array) works because someone made it work or because of a happy coincidence. |
It’s consistent with normal assignability rules, given what types result in both cases. The fact that the |
fwiw the lines between “works by happy coincidence” and “someone made it work this way” tend to be blurred in a structural type system because the compiler only cares about the contents of your type and not what keywords were used to construct it. Higher-order expectations don’t really translate well in that environment, and you often have to trade one inconsistency for another. |
well, you learn something new every day. this doesn't seem like it will be changed, so I'll close this issue |
Bug Report
🔎 Search Terms
readonly
🕗 Version & Regression Information
I saw this behavior on TypeScript 4.9.4.
⏯ Playground Link
bug workbench with relevant code
💻 Code
🙁 Actual behavior
I noticed that a readonly record can be assigned to a mutable record without a compile error. I assume this is because everything is assignable to the empty object (as described here) but it is very unintuitive, since we are not actively creating a class or type with no properties. If you ask any developer, they would assume this code to produce an error.
🙂 Expected behavior
Optimally, the assignment of an immutable record into a mutable record variable should not be allowed.
The text was updated successfully, but these errors were encountered: