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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//// [abstractPropertyInConstructor.ts]
abstract class AbstractClass {
constructor(str: string, other: AbstractClass) {
this.method(parseInt(str));
let val = this.prop.toLowerCase();
if (!str) {
this.prop = "Hello World";
}
this.cb(str);
// OK, reference is inside function
const innerFunction = () => {
return this.prop;
}
// OK, references are to another instance
other.cb(other.prop);
}
abstract prop: string;
abstract cb: (s: string) => void;
abstract method(num: number): void;
other = this.prop;
fn = () => this.prop;
method2() {
this.prop = this.prop + "!";
}
}
abstract class DerivedAbstractClass extends AbstractClass {
cb = (s: string) => {};
constructor(str: string, other: AbstractClass, yetAnother: DerivedAbstractClass) {
super(str, other);
// there is no implementation of 'prop' in any base class
this.cb(this.prop.toLowerCase());
this.method(1);
// OK, references are to another instance
other.cb(other.prop);
yetAnother.cb(yetAnother.prop);
}
}
class Implementation extends DerivedAbstractClass {
prop = "";
cb = (s: string) => {};
constructor(str: string, other: AbstractClass, yetAnother: DerivedAbstractClass) {
super(str, other, yetAnother);
this.cb(this.prop);
}
method(n: number) {
this.cb(this.prop + n);
}
}
class User {
constructor(a: AbstractClass) {
a.prop;
a.cb("hi");
a.method(12);
a.method2();
}
}
abstract class C1 {
abstract x: string;
abstract y: string;
constructor() {
let self = this; // ok
let { x, y: y1 } = this; // error
({ x, y: y1, "y": y1 } = this); // error
}
}
class C2 {
x: string;
y: string;
constructor() {
let self = this; // ok
let { x, y: y1 } = this; // ok
({ x, y: y1, "y": y1 } = this); // ok
}
}
//// [abstractPropertyInConstructor.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var AbstractClass = /** @class */ (function () {
function AbstractClass(str, other) {
var _this = this;
this.other = this.prop;
this.fn = function () { return _this.prop; };
this.method(parseInt(str));
var val = this.prop.toLowerCase();
if (!str) {
this.prop = "Hello World";
}
this.cb(str);
// OK, reference is inside function
var innerFunction = function () {
return _this.prop;
};
// OK, references are to another instance
other.cb(other.prop);
}
AbstractClass.prototype.method2 = function () {
this.prop = this.prop + "!";
};
return AbstractClass;
}());
var DerivedAbstractClass = /** @class */ (function (_super) {
__extends(DerivedAbstractClass, _super);
function DerivedAbstractClass(str, other, yetAnother) {
var _this = _super.call(this, str, other) || this;
_this.cb = function (s) { };
// there is no implementation of 'prop' in any base class
_this.cb(_this.prop.toLowerCase());
_this.method(1);
// OK, references are to another instance
other.cb(other.prop);
yetAnother.cb(yetAnother.prop);
return _this;
}
return DerivedAbstractClass;
}(AbstractClass));
var Implementation = /** @class */ (function (_super) {
__extends(Implementation, _super);
function Implementation(str, other, yetAnother) {
var _this = _super.call(this, str, other, yetAnother) || this;
_this.prop = "";
_this.cb = function (s) { };
_this.cb(_this.prop);
return _this;
}
Implementation.prototype.method = function (n) {
this.cb(this.prop + n);
};
return Implementation;
}(DerivedAbstractClass));
var User = /** @class */ (function () {
function User(a) {
a.prop;
a.cb("hi");
a.method(12);
a.method2();
}
return User;
}());
var C1 = /** @class */ (function () {
function C1() {
var _a;
var self = this; // ok
var _b = this, x = _b.x, y1 = _b.y; // error
(_a = this, x = _a.x, y1 = _a.y, y1 = _a["y"]); // error
}
return C1;
}());
var C2 = /** @class */ (function () {
function C2() {
var _a;
var self = this; // ok
var _b = this, x = _b.x, y1 = _b.y; // ok
(_a = this, x = _a.x, y1 = _a.y, y1 = _a["y"]); // ok
}
return C2;
}());
|