|
| 1 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(7,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 2 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(8,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 3 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(9,18): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 4 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(20,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 5 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(21,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 6 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(22,22): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 7 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(30,30): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 8 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(39,22): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 9 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(43,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 10 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(44,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 11 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(45,18): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 12 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(59,27): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 13 | +tests/cases/compiler/checkSuperCallBeforeThisAccess.ts(75,27): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 14 | + |
| 15 | + |
| 16 | +==== tests/cases/compiler/checkSuperCallBeforeThisAccess.ts (13 errors) ==== |
| 17 | + class A { |
| 18 | + x = 1; |
| 19 | + } |
| 20 | + |
| 21 | + class C1 extends A { |
| 22 | + constructor(n: number) { |
| 23 | + let a1 = this; // Error |
| 24 | + ~~~~ |
| 25 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 26 | + let a2 = this.x; // Error |
| 27 | + ~~~~ |
| 28 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 29 | + let a3 = super.x; // Error |
| 30 | + ~~~~~ |
| 31 | +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 32 | + let a4 = () => this; |
| 33 | + let a5 = () => this.x; |
| 34 | + let a6 = () => super.x; |
| 35 | + if (!!true) { |
| 36 | + super(); |
| 37 | + let b1 = this; |
| 38 | + let b2 = this.x; |
| 39 | + let b3 = super.x; |
| 40 | + } |
| 41 | + else { |
| 42 | + let c1 = this; // Error |
| 43 | + ~~~~ |
| 44 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 45 | + let c2 = this.x; // Error |
| 46 | + ~~~~ |
| 47 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 48 | + let c3 = super.x; // Error |
| 49 | + ~~~~~ |
| 50 | +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 51 | + } |
| 52 | + if (!!true) { |
| 53 | + switch (n) { |
| 54 | + case 1: |
| 55 | + super(); |
| 56 | + let d1 = this.x; |
| 57 | + case 2: |
| 58 | + let d2 = this.x; // Error |
| 59 | + ~~~~ |
| 60 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 61 | + default: |
| 62 | + super(); |
| 63 | + let d3 = this.x; |
| 64 | + } |
| 65 | + let d4 = this.x; |
| 66 | + } |
| 67 | + if (!!true) { |
| 68 | + let e1 = { w: !!true ? super() : 0 }; |
| 69 | + let e2 = this.x; // Error |
| 70 | + ~~~~ |
| 71 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 72 | + let e3 = { w: !!true ? super() : super() }; |
| 73 | + let e4 = this.x; |
| 74 | + } |
| 75 | + let f1 = this; // Error |
| 76 | + ~~~~ |
| 77 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 78 | + let f2 = this.x; // Error |
| 79 | + ~~~~ |
| 80 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 81 | + let f3 = super.x; // Error |
| 82 | + ~~~~~ |
| 83 | +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Repro from #38512 |
| 88 | + |
| 89 | + export class Foo { |
| 90 | + constructor(value: number) { |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + export class BarCorrectlyFails extends Foo { |
| 95 | + constructor(something: boolean) { |
| 96 | + if (!something) { |
| 97 | + const value = this.bar(); // Error |
| 98 | + ~~~~ |
| 99 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 100 | + super(value); |
| 101 | + } |
| 102 | + else { |
| 103 | + super(1337); |
| 104 | + } |
| 105 | + } |
| 106 | + bar(): number { return 4; } |
| 107 | + } |
| 108 | + |
| 109 | + export class BarIncorrectlyWorks extends Foo { |
| 110 | + constructor(something: boolean) { |
| 111 | + if (something) { |
| 112 | + super(1337); |
| 113 | + } |
| 114 | + else { |
| 115 | + const value = this.bar(); // Error |
| 116 | + ~~~~ |
| 117 | +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. |
| 118 | + super(value); |
| 119 | + } |
| 120 | + } |
| 121 | + bar(): number { return 4; } |
| 122 | + } |
| 123 | + |
0 commit comments