以下打字稿代码会导致错误(打字稿 v3.8.3)。为什么?
class A {
public foobar: 'foo' | 'bar' = 'bar';
}
class B extends A {
public foobar = 'foo'; // --> Type 'string' is not assignable to type '"foo" | "bar"'
}
我在下面提供了这个问题的答案,但我很高兴听到替代方案来实现不需要编写构造函数或在子类中复制类型定义的相同目的。