-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Interface with readonly property is assignable to interface with mutable property #13347
Comments
Judging from #6614 there was an idea to add a interface MutableValue<T> {
mutable value: T;
}
interface ImmutableValue<T> {
readonly value: T;
} and that would stop |
@danielearwicker I think you are channeling @Aleksey-Bykov and his issue readonly modifiers are a joke |
Sometimes I think I'm way too polite! |
I don't see an obvious "vote for this" button, so count this as my +1. Just ran into a nasty bug of mine where I was incorrectly modifying a member of a class instance, so I changed the type using ReadOnly, hoping I would get a compile error to quickly point my lazy self to there I was assigning it to a "normal" T, but no luck. As far as I am concerned, there is no difference between allowing an assignment of a type ReadOnly to a T and allowing an assignment of an Object to, say, an Array. |
@DavidKDeutsch In the upper-right corner of every comment, there is a +:grinning: button. If you click that, you can choose the :+1: reaction. This counts as a plus vote. You can do this on any comment, but often it is done on the first comment. |
Here's a repro demonstrating the issue online. |
The correct behavior in that repro would have the fifth line ( |
Has adding this behavior behind a configuration flag been already discussed internally or externally? If so, what has been the outcome of that? I think adding this behavior as a feature flag would encourage people to try this and work with library definitions to add |
@RyanCavanaugh Is there any progress? |
I'd love to see if there's any progress on this. I was just teaching some engineers about TypeScript today, showing different aspects of interfaces, optional and readonly properties. I went a little off my script and showed something like above....which didn't work how I would have expected. I'd love to see the readonly constraint respected. It's particularly relevant for some of the work we're doing on the vNext of Aurelia, which is all TypeScript. We also have a lot of immutable scenarios in my day job's codebase where it would be nice to get some help from the compiler... |
I think this is more likely to come up when calling a mutating function: interface Foo {
id: string;
}
const foo: Readonly<Foo> = {
id: 'original ID',
}
function mutateFoo(foo: Foo) {
foo.id = 'new ID';
}
mutateFoo(foo); As in the prior cases, it's "obvious" that a coercion is happening, here If type Mutable<T> = {
-readonly [k in typeof T]: T[k]
}
foo = mutateFoo(Mutable<foo>); // Hey look I know foo will be mutated and explicitly opted in! |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
Returning to the code sample in the first post... |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Hey folks, let's dial it down a notch. Remember the principles of the code of conduct In no particular order:
I don't see any block-worthy behavior here but multiple people are quite close to crossing that line and I'd obviously prefer to not have to do that. Thanks! |
@RyanCavanaugh - can you please comment on the status of this bug from the project's perspective? I don't see any official comment from the ts team. |
It's in the long list of features which are plausible, but aren't currently a priority based on the overall tradeoff of what would be gained here (write safety under aliasing, which is already a soundness hole as relates to the types of the properties themselves) vs what would be required for anyone to be able to turn the option on (generally speaking, "everyone" would have to properly document |
It’s a chicken and egg problem |
This is a pretty massive hole. What do we have to do to get some movement on this? Does anyone know why this keyword was added when it provides next to no protection? EDIT: To be clear, I am asking if someone could point me towards the PR or issue that led to the addition of this feature so I can establish what the intention was, and how it ended up in the state that it's in. I am sure it was done with good intentions, and it seems unfortunate that it is stuck half way. |
Relevant comment by RyanCavanaugh: #58236 (comment)
|
Now implemented in #58296. |
TypeScript Version: 2.1.4
Code
Expected behavior:
The assignment of
i
tom
would fail, to stop us accidentally allowingvalue
to be modified.Actual behavior:
The assignment is allowed.
The current behaviour was a deliberate choice so this is a breaking change (or strict flag) feature request rather than a bug report!
The Handbook has this snippet:
It notes that
a = ro
being an error is helpful. But this happens becauseReadonlyArray
has nopush
method, making it incompatible withArray
.My example above seems "morally equivalent" to modelling the input/output flow of values with separate methods:
And sure enough, this stops the assignment of
i
tom
.Would 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).
The text was updated successfully, but these errors were encountered: