Skip to content

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

Closed
@sceutre

Description

@sceutre

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions