Interface with readonly property is assignable to interface with mutable property
**TypeScript Version:** 2.1.4 **Code** ```ts interface MutableValue<T> { value: T; } interface ImmutableValue<T> { readonly value: T; } let i: ImmutableValue<string> = { value: "hi" }; i.value = "Excellent, I can't change it"; // compile-time error let m: MutableValue<string> = i; m.value = "Oh dear, I can change it"; ``` **Expected behavior:** The assignment of `i` to `m` would fail, to stop us accidentally allowing `value` to be modified. **Actual behavior:** The assignment is allowed. [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! The Handbook [has this snippet](https://www.typescriptlang.org/docs/handbook/interfaces.html): ```ts let a: number[] = [1, 2, 3, 4]; let ro: ReadonlyArray<number> = a; ro[0] = 12; // error! ro.push(5); // error! ro.length = 100; // error! a = ro; // error! ``` It notes that `a = ro` being an error is helpful. But this happens because `ReadonlyArray` has no `push` method, making it incompatible with `Array`. My example above seems "morally equivalent" to modelling the input/output flow of values with separate methods: ```ts interface MutableValue<T> { getValue(): T; setValue(v: T): void; } interface ImmutableValue<T> { getValue(): T; } declare let i: ImmutableValue<string>; i.setValue("Excellent, I can't change it"); // compile-time error let m: Mutab
Posted by danielearwicker on Jan 7, 2017, 11:47 PM
Tags
Related issues
- [BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
- Feature Request: Auto-resume CLI session when usage limit resets
- Please deobfuscate the Python
- [Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
- # 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
- [FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
reliability (Software)
[BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
Feature Request: Auto-resume CLI session when usage limit resets
Please deobfuscate the Python
[Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
# 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
[FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Admin Materials and Surfaces
AMD Support
Interface with readonly property is assignable to interface with mutable property
**TypeScript Version:** 2.1.4 **Code** ```ts interface MutableValue<T> { value: T; } interface ImmutableValue<T> { readonly value: T; } let i: ImmutableValue<string> = { value: "hi" }; i.value = "Excellent, I can't change it"; // compile-time error let m: MutableValue<string> = i; m.value = "Oh dear, I can change it"; ``` **Expected behavior:** The assignment of `i` to `m` would fail, to stop us accidentally allowing `value` to be modified. **Actual behavior:** The assignment is allowed. [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! The Handbook [has this snippet](https://www.typescriptlang.org/docs/handbook/interfaces.html): ```ts let a: number[] = [1, 2, 3, 4]; let ro: ReadonlyArray<number> = a; ro[0] = 12; // error! ro.push(5); // error! ro.length = 100; // error! a = ro; // error! ``` It notes that `a = ro` being an error is helpful. But this happens because `ReadonlyArray` has no `push` method, making it incompatible with `Array`. My example above seems "morally equivalent" to modelling the input/output flow of values with separate methods: ```ts interface MutableValue<T> { getValue(): T; setValue(v: T): void; } interface ImmutableValue<T> { getValue(): T; } declare let i: ImmutableValue<string>; i.setValue("Excellent, I can't change it"); // compile-time error let m: Mutab
Comments (0)
No comments yet. Start the discussion.