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

Using Readonly<Class> makes a type incompatible only if there are private fields #16441

Closed
sceutre opened this issue Jun 11, 2017 · 3 comments
Closed
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@sceutre
Copy link

sceutre commented Jun 11, 2017

TypeScript Version: 2.3.2

Code

class A { public x:number; }
class B { private x:number; }

let a:A = new A as Readonly<A>; // succeeds
let b:B = new B as Readonly<B>; // type error: Property x is missing in Readonly<B>

class AA { private x:number; }
class BB extends AA {}

let bb:BB = new BB as Readonly<BB>; // type error: Property x is missing in Readonly<BB>

Expected behavior:

To me it appears B and its Readonly are the same (since there are no external visible properties) while A and its Readonly differ in the mutability in x. Although I also think it would be fine if all assignments worked.

I actually noticed this with the case of BB and it being a private field in the super class that caused the assignment to fail.

Actual behavior:

As per code comments

@mhegazy
Copy link
Contributor

mhegazy commented Jun 12, 2017

mapped types, which Readonly is one, only operate on public fields of a type.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 12, 2017
@sceutre
Copy link
Author

sceutre commented Jun 12, 2017

Ah ok, I was thinking (not very deeply) that the issue was more that the type of a class should similarly only include its public members.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
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

2 participants