在 TypeScript 中会出现错误...
interface A {
getSomeBasicA():A;
getSomeBetterA():BetterA;
}
interface BetterA extends A {
getSomeBasicA():BetterA;
doFancyAStuff():void;
}
interface B extends A {
getSomeBetterA():BetterB;
}
interface BetterB extends B, BetterA {}
... with this message ...
error TS2430: Interface 'B' incorrectly extends interface 'A'.
The types returned by 'getSomeBetterA().getSomeBasicA()' are incompatible between these types.
Property 'doFancyAStuff' is missing in type 'A' but required in type 'BetterA'.
error TS2320: Interface 'BetterB' cannot simultaneously extend types 'B' and 'BetterA'.
Named property 'getSomeBasicA' of types 'B' and 'BetterA' are not identical.
error TS2320: Interface 'BetterB' cannot simultaneously extend types 'B' and 'BetterA'.
Named property 'getSomeBetterA' of types 'B' and 'BetterA' are not identical.
虽然这在Java中可行,但...
public interface A {
public A getSomeBasicA();
public BetterA getSomeBetterA();
}
public interface BetterA extends A {
public BetterA getSomeBasicA();
public void doFancyAStuff();
}
public interface B extends A {
public BetterB getSomeBetterA();
}
public interface BetterB extends B, BetterA {
}
我不明白为什么会出现这种情况?难道这不是类型安全的(它如何被破坏,为什么Java编译器允许这种情况)?有没有一个名称可以用来描述一个编译器支持而另一个编译器不支持的功能?