TypeScript 类:静态和继承

TypeScript class: statics and inheritance

问题:

  1. 我观察到的行为是 TypeScript 的预期行为吗?
  2. 我观察到的行为是 ECMAScript 6 的预期行为吗?
  3. 是否有一种简单的方法可以通过继承层次结构回溯处理每个级别的 'myStatic' 数组?我怎么知道什么时候停止?

描述:使用 TypeScript 时,派生类和静态属性似乎有一些有趣的行为。

TypeScript Example

class MyBase {
    static myStatic = [];
}

class MyDerived extends MyBase {}

MyBase.myStatic = ['one', 'two', 'three']

class MyDerived2 extends MyBase {}

document.body.innerHTML += "<b>MyDerived.myStatic:</b> " + JSON.stringify(MyDerived.myStatic) + "<br/>";
document.body.innerHTML += "<b>MyDerived2.myStatic:</b> " + JSON.stringify(MyDerived2.myStatic) + "<br/>";

这是结果:

MyDerived.myStatic: []
MyDerived2.myStatic: ["one","two","three"]

编辑:添加说明 TypeScript 和 ECMA Script 6 之间不同行为的示例。注意:ECMA Script 不支持静态属性,因此这些示例使用静态方法。

TypeScript 代码:

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {}

MyBase.myStatic = () => { return ['one', 'two', 'three']; }

class MyDerived2 extends MyBase {}

document.body.innerHTML += "<b>MyDerived.myStatic:</b> " + JSON.stringify(MyDerived.myStatic()) + "<br/>";
document.body.innerHTML += "<b>MyDerived2.myStatic:</b> " + JSON.stringify(MyDerived2.myStatic()) + "<br/>";

TypeScript 结果:

MyDerived.myStatic: []
MyDerived2.myStatic: ["one","two","three"]

ECMA 脚本 6 代码:ES6 Fiddle

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {}

MyBase.myStatic = () => { return ['one', 'two', 'three']; };

class MyDerived2 extends MyBase {}

console.log("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()));
console.log("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()));

ECMA 脚本 6 结果

MyDerived.myStatic: ["one","two","three"]
MyDerived2.myStatic: ["one","two","three"]

Is the behavior I'm observing the expected behavior for TypeScript? Is the behavior I'm observing the expected behavior for ECMA Script 6?

是的,是的。 类 允许 运行时修改 并按定义顺序处理。 inherit 仅在定义点开始,因此取决于基础 在该点 的属性。

See related TypeScript bug report

以下是具有一致行为并遍历继承层次结构的 TypeScript 和 ES6 代码示例:

TypeScript 代码:

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {
  static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
}

MyBase.myStatic = () => { return ['one', 'two', 'three']; };

class MyDerived2 extends MyBase {
  static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
}

document.body.innerHTML += ("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()) + "<br/>");
document.body.innerHTML += ("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()) + "<br/>");

TypeScript 结果:

MyDerived.myStatic: ["mickey","goofy",["one","two","three"]]
MyDerived2.myStatic: ["mickey","goofy",["one","two","three"]]

ES6代码:ES6 Fiddle

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {
  static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
}

MyBase.myStatic = () => { return ['one', 'two', 'three']; };

class MyDerived2 extends MyBase {
  static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
}

console.log("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()));
console.log("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()));

ES6 结果

MyDerived.myStatic: ["mickey","goofy",["one","two","three"]]
MyDerived2.myStatic: ["mickey","goofy",["one","two","three"]]