【问题标题】:Restricting generic types to one of several classes in Typescript将泛型类型限制为 Typescript 中的几个类之一
【发布时间】:2023-03-06 23:59:02
【问题描述】:

在 Typescript 中,如何在编译时将泛型类型限制为多个类之一?比如,你如何实现这个伪代码?

class VariablyTyped<T has one of types A or B or C> {

    method(hasOneType: T) {
        if T has type A:
            do something assuming type A
        if T has type B:
            do something assuming type B
        if T has type C:
            do something assuming type C
    }
}

此外,我希望能够将属性(或任何变量)分配给泛型类型选项之一的特定后代类型,而不仅仅是给定类型之一。例如:

class VariablyTyped<T has one of types A or B or C> {

    descendentClassOfT: T

    method(hasOneType: T) {
        descendentClassOfT = hasOneType
    }
}

class D extends class C {
    methodUniqueToD() { }
}

const v = new VariablyTyped(new D())
v.descendentClassOfT.methodUniqueToD()

这个答案显然并不明显,因为我花了几个小时来解决这个问题。在我看来,这个问题的某种形式had already been asked,但给出的解决方案甚至不适合我。可能只是在非常具体的情况下才回答了之前的问题,因为赞成票的数量表明它正在解决某些人的问题。

我发布这个新问题是为了清楚地说明一般问题并跟进解决方案。

【问题讨论】:

    标签: typescript generics restriction typescript-generics


    【解决方案1】:

    我为此花了几个小时的时间,但回想起来,解决方案似乎很明显。首先我介绍解决方案,然后将其与之前的方法进行比较。 (在 Typescript 2.6.2 中测试。)

    // WORKING SOLUTION: union of types with type checks
    
    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();
            }
            // either type-check here (as implemented) or typecast (commented out)
            else if (hasOneType instanceof OrOfThisBaseType) {
                hasOneType.method3a();
                // (<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;
    

    将上述方法与声明泛型为类选项交集的previously accepted answer 进行比较:

    // NON-WORKING SOLUTION A: intersection of types
    
    class BadVariablyTyped_A<T extends MustBeThis & OrThis & OrOfThisBaseType> {
        extendsBaseType: T;
    
        constructor(hasOneType: T) {
            if (hasOneType instanceof MustBeThis) {
                (<MustBeThis>hasOneType).method1();
            }
            // ERROR: The left-hand side of an 'instanceof' expression must be of type
            // 'any', an object type or a type parameter. (parameter) hasOneType: never
            else if (hasOneType instanceof OrThis) {
                (<OrThis>hasOneType).method2();
            }
            else {
                (<OrOfThisBaseType>hasOneType).method3a();
                this.extendsBaseType = hasOneType;
            }
        }
    }
    
    // ERROR: Property 'method2' is missing in type 'MustBeThis'.
    const b1_A = new BadVariablyTyped_A(new MustBeThis());
    // ERROR: Property 'method2' is missing in type 'MustBeThis'.
    const b1t_A = new BadVariablyTyped_A<MustBeThis>(new MustBeThis());
    
    // ERROR: Property 'method1' is missing in type 'OrThis'.
    const b2_A = new BadVariablyTyped_A(new OrThis());
    // ERROR: Property 'method1' is missing in type 'OrThis'.
    const b2t_A = new BadVariablyTyped_A<OrThis>(new OrThis());
    
    // ERROR: Property 'method1' is missing in type 'ExtendsBaseType'.
    const b3_A = new BadVariablyTyped_A(new ExtendsBaseType());
    // ERROR: Property 'method1' is missing in type 'ExtendsBaseType'.
    const b3t_A = new BadVariablyTyped_A<ExtendsBaseType>(new ExtendsBaseType());
    

    还将上述工作方法与another suggested solution 进行比较,其中泛型类型被限制为扩展实现所有类接口选项的接口。这里发生的错误表明它在逻辑上与之前的非工作解决方案相同。

    // NON-WORKING SOLUTION B: multiply-extended interface
    
    interface VariableType extends MustBeThis, OrThis, OrOfThisBaseType { }
    
    class BadVariablyTyped_B<T extends VariableType> {
        extendsBaseType: T;
    
        constructor(hasOneType: T) {
            if (hasOneType instanceof MustBeThis) {
                (<MustBeThis>hasOneType).method1();
            }
            // ERROR: The left-hand side of an 'instanceof' expression must be of type
            // 'any', an object type or a type parameter. (parameter) hasOneType: never
            else if (hasOneType instanceof OrThis) {
                (<OrThis>hasOneType).method2();
            }
            else {
                (<OrOfThisBaseType>hasOneType).method3a();
                this.extendsBaseType = hasOneType;
            }
        }
    }
    
    // ERROR: Property 'method2' is missing in type 'MustBeThis'.
    const b1_B = new BadVariablyTyped_B(new MustBeThis());
    // ERROR: Property 'method2' is missing in type 'MustBeThis'.
    const b1t_B = new BadVariablyTyped_B<MustBeThis>(new MustBeThis());
    
    // ERROR: Property 'method1' is missing in type 'OrThis'.
    const b2_B = new BadVariablyTyped_B(new OrThis());
    // ERROR: Property 'method1' is missing in type 'OrThis'.
    const b2t_B = new BadVariablyTyped_B<OrThis>(new OrThis());
    
    // ERROR: Property 'method1' is missing in type 'ExtendsBaseType'.
    const b3_B = new BadVariablyTyped_B(new ExtendsBaseType());
    // ERROR: Property 'method1' is missing in type 'ExtendsBaseType'.
    const bt_B = new BadVariablyTyped_B<ExtendsBaseType>(new ExtendsBaseType());
    

    具有讽刺意味的是,我后来解决了我的应用程序特定问题,而不必限制泛型类型。也许其他人应该吸取我的教训,首先尝试找到另一种更好的方法来完成这项工作。

    【讨论】:

    • 我将另一个问题解释为要求交集:满足 all 多个约束的单一类型。您要求联合:满足至少一个多个约束的单一类型。 (对这个问题的回答在某些地方使用“联合”这个词来表示“交叉点”并没有帮助)。因此,另一个问题的解决方案对您不起作用是有道理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多