Skip to content
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

Passing object with readonly fields to function expecting modifiable fields does not give a type error #11180

Closed
paulkoerbitz opened this issue Sep 27, 2016 · 7 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@paulkoerbitz
Copy link
Contributor

TypeScript Version: 2.0.3, 2.1.0-dev.20160927

Code

// A *self-contained* demonstration of the problem follows...
'use strict';

interface RO { readonly x: number; }
interface RW { x: number; }

const ro: RO = Object.freeze({ x: 3 });

function modifyX(input: RW) {
    input.x = 5;
}

// No type error
// Throws exception
modifyX(ro);

Expected behavior:
The call modifyX(ro) should give a type error, as ro has a readonly property but the interface to modifyX requires a modifiable field.

Actual behavior:
No type error.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 27, 2016
@RyanCavanaugh
Copy link
Member

To avoid breaking every definition file in the world, the opposite of readonly is sort of "unknown", not "must be mutable". readonly doesn't affect type relationships, it just disallows the line ro.x = 1; in that sample.

@paulkoerbitz
Copy link
Contributor Author

OK, I understand that backwards compatibility is important, but I do think that it limits the usefulness of readonly somewhat. I would like to use it particularly to rule out changing frozen objects (which are frozen for immutability requirements). I had hoped to be able to use readonly to do so, but if the behavior above is as intended, I don't see how I can achieve that.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 27, 2016

we have talked about --strictReadOnlychecks in the past that will treat "unknown" as "read-write". I do not think we have an issue tracking this though.

@aluanhaddad
Copy link
Contributor

I would definitely love to see a --strictReadOnlychecks option.

@paulkoerbitz
Copy link
Contributor Author

@mhegazy would it be apropriate for me to open a suggestion for this? (I am obviously +1 on this ;) )

@paulkoerbitz
Copy link
Contributor Author

I have opened #11481 as a suggestion to add strictReadonlyChecks

@mhegazy
Copy link
Contributor

mhegazy commented Oct 14, 2016

closing in favor of #11481

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants