1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
=== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyConstructorAssignment.ts ===
// Tests that readonly parameter properties behave like regular readonly properties
class A {
>A : A
constructor(readonly x: number) {
>x : number
this.x = 0;
>this.x = 0 : 0
>this.x : number
>this : this
>x : number
>0 : 0
}
}
class B extends A {
>B : B
>A : A
constructor(x: number) {
>x : number
super(x);
>super(x) : void
>super : typeof A
>x : number
// Fails, x is readonly
this.x = 1;
>this.x = 1 : 1
>this.x : any
>this : this
>x : any
>1 : 1
}
}
class C extends A {
>C : C
>A : A
// This is the usual behavior of readonly properties:
// if one is redeclared in a base class, then it can be assigned to.
constructor(readonly x: number) {
>x : number
super(x);
>super(x) : void
>super : typeof A
>x : number
this.x = 1;
>this.x = 1 : 1
>this.x : number
>this : this
>x : number
>1 : 1
}
}
class D {
>D : D
constructor(private readonly x: number) {
>x : number
this.x = 0;
>this.x = 0 : 0
>this.x : number
>this : this
>x : number
>0 : 0
}
}
// Fails, can't redeclare readonly property
class E extends D {
>E : E
>D : D
constructor(readonly x: number) {
>x : number
super(x);
>super(x) : void
>super : typeof D
>x : number
this.x = 1;
>this.x = 1 : 1
>this.x : number
>this : this
>x : number
>1 : 1
}
}
|