這似乎是一個錯誤,因爲即使當extends
是用來代替implements
這是行不通的,但這個工作得很好:
class Test {
someObj: {
someString: 'this'|'that'|'the other'
};
}
class Test2 extends Test {
constructor() {
super();
this.someObj = {
someString: 'this'
}
}
}
而且都編譯成相同的js:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Test = (function() {
function Test() {
}
return Test;
}());
var Test2 = (function (_super) {
__extends(Test2, _super);
function Test2() {
_super.call(this);
this.someObj = {
someString: 'this'
};
}
return Test2;
}(Test));
(除了th在你的代碼中是:_super.apply(this, arguments)
)。
你可以在their github上發佈一個問題,如果你這樣做,請將url作爲評論發佈,因爲我想跟隨它。
如果你真的打算用implements
,而不是extends
然後使用構造不會解決你的問題,但這樣做會:
type SomeObj = {
someString: 'this'|'that'|'the other'
};
class Test {
someObj: SomeObj;
}
class Test2 implements Test {
someObj: SomeObj = {
someString: 'this'
}
}
感謝尋找到這進一步Nitzan。這是問題:https://github.com/Microsoft/TypeScript/issues/10071 – dsifford
太好了,謝謝你的鏈接 –