我曾经花费了几个小时来解决这个问题,但回想起来,解决方案似乎很明显。首先,我介绍一下解决方案,然后再将其与以前的方法进行比较。(在Typescript 2.6.2中测试过。)
class MustBeThis {
method1() { }
}
class OrThis {
method2() { }
}
abstract class OrOfThisBaseType {
method3a() { }
}
class ExtendsBaseType extends OrOfThisBaseType {
method3b() { }
}
class GoodVariablyTyped<T extends MustBeThis | OrThis | OrOfThisBaseType> {
extendsBaseType: T;
constructor(hasOneType: T) {
if (hasOneType instanceof MustBeThis) {
hasOneType.method1();
}
else if (hasOneType instanceof OrThis) {
hasOneType.method2();
}
else if (hasOneType instanceof OrOfThisBaseType) {
hasOneType.method3a();
this.extendsBaseType = hasOneType;
}
}
}
这个解决方案的以下检查编译得非常好:
const g1 = new GoodVariablyTyped(new MustBeThis());
const g1t = new GoodVariablyTyped<MustBeThis>(new MustBeThis());
const g1e: MustBeThis = g1.extendsBaseType;
const g1te: MustBeThis = g1t.extendsBaseType;
const g2 = new GoodVariablyTyped(new OrThis());
const g2t = new GoodVariablyTyped<OrThis>(new OrThis());
const g2e: OrThis = g2.extendsBaseType;
const g2te: OrThis = g2t.extendsBaseType;
const g3 = new GoodVariablyTyped(new ExtendsBaseType());
const g3t = new GoodVariablyTyped<ExtendsBaseType>(new ExtendsBaseType());
const g3e: ExtendsBaseType = g3.extendsBaseType;
const g3te: ExtendsBaseType = g3t.extendsBaseType;
与之前被接受的答案相比,上述方法将泛型声明为类选项的交集,请参考
以前被接受的答案。
class BadVariablyTyped_A<T extends MustBeThis & OrThis & OrOfThisBaseType> {
extendsBaseType: T;
constructor(hasOneType: T) {
if (hasOneType instanceof MustBeThis) {
(<MustBeThis>hasOneType).method1();
}
else if (hasOneType instanceof OrThis) {
(<OrThis>hasOneType).method2();
}
else {
(<OrOfThisBaseType>hasOneType).method3a();
this.extendsBaseType = hasOneType;
}
}
}
const b1_A = new BadVariablyTyped_A(new MustBeThis());
const b1t_A = new BadVariablyTyped_A<MustBeThis>(new MustBeThis());
const b2_A = new BadVariablyTyped_A(new OrThis());
const b2t_A = new BadVariablyTyped_A<OrThis>(new OrThis());
const b3_A = new BadVariablyTyped_A(new ExtendsBaseType());
const b3t_A = new BadVariablyTyped_A<ExtendsBaseType>(new ExtendsBaseType());
还可以将上述工作方法与另一种建议的解决方案进行比较,其中通用类型被限制为扩展实现类接口选项的接口。这里发生的错误表明它在逻辑上与先前无法工作的解决方案相同。
interface VariableType extends MustBeThis, OrThis, OrOfThisBaseType { }
class BadVariablyTyped_B<T extends VariableType> {
extendsBaseType: T;
constructor(hasOneType: T) {
if (hasOneType instanceof MustBeThis) {
(<MustBeThis>hasOneType).method1();
}
else if (hasOneType instanceof OrThis) {
(<OrThis>hasOneType).method2();
}
else {
(<OrOfThisBaseType>hasOneType).method3a();
this.extendsBaseType = hasOneType;
}
}
}
const b1_B = new BadVariablyTyped_B(new MustBeThis());
const b1t_B = new BadVariablyTyped_B<MustBeThis>(new MustBeThis());
const b2_B = new BadVariablyTyped_B(new OrThis());
const b2t_B = new BadVariablyTyped_B<OrThis>(new OrThis());
const b3_B = new BadVariablyTyped_B(new ExtendsBaseType());
const bt_B = new BadVariablyTyped_B<ExtendsBaseType>(new ExtendsBaseType());
具有讽刺意味的是,我后来解决了应用程序特定的问题,而不必限制泛型类型。也许其他人应该从我的经验中吸取教训,首先尝试找到另一种更好的方法来完成工作。