- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 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 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 进行比较其中泛型类型被限制为扩展一个实现所有类接口(interface)选项的接口(interface)。此处发生的错误表明它在逻辑上与先前的无效解决方案相同。
// 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());
具有讽刺意味的是,我后来解决了我的特定于应用程序的问题,而不必限制通用类型。也许其他人应该吸取我的教训,首先尝试找到另一种更好的方法来完成这项工作。
关于typescript - 将泛型类型限制为 Typescript 中的几个类之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48539710/
可以用这种方式转换字符串吗?我们有相同的参数,Java 做出了正确的选择。如果值是整数 - 我们调用 parseInt(value),否则如果值是 double 型 - 我们调用 parseDoubl
如果这段代码中有一个愚蠢的错误,我提前道歉,但我似乎无法解决它。我的问题是这样的,我用GCC-8(通过home-brew安装在Mac上)编译,然后在终端中执行。当使用 int do 定义变量 s &
我用 a-videosphere 制作了一个 a-scene。我尝试使用按钮启用/禁用声音,但有些想法不起作用?这是我的代码: var gargamel = 0; function
我正在使用 ISAAC 实现来生成随机整数。我需要用这些整数创建一个高斯值。首先,我需要将它们从 0 更改为 1 的 double 值。我怎样才能在Java中做到这一点?这是到目前为止我将整数转换为
我将 0x0000 到 0x01c2 范围内的十六进制值从 BLE 获取到我的手机 a 作为字符串。为了将其绘制在图表中,我必须将其转换为 double,我已经尝试过 this method但遗憾的是
我有一个父类(super class) Animal和一个子类 Dog 。在第三节课中,我有一个 List它同时接受子类型和父类(super class)型对象。 public class foo{
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我正在尝试查询我的用户的距离。我可以用这段代码做到这一点 PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGe
考虑示例:http://jsfiddle.net/KWLu7/16/ 我正在尝试将总体重要性值计算为每个选定选择重要性的总和乘以其父标准重要性: var watch = $scope.$watch("
这个问题在这里已经有了答案: Bounding generics with 'super' keyword (6 个答案) 关闭 2 年前。 我有一个列表装饰器,它应该允许从一个列表转换到另一个列表
为什么下面的代码没有选择最近父类(super class)型的隐式 val? class A class B extends A trait TC[-T] { def show(t: T): Stri
这是我想要做的 def merge[A, B, C](eithers: Either[A,B]*)(implicit ev1: A x, x => x)) 关于scala - 推断常见的父类(s
我正在尝试从具有 double 类型列的Cassandra表中获取 double 值。我已经使用CQL3语法创建了表: CREATE TABLE data_double ( datetime
是否应该在不需要显式类型定义的情况下编译以下 this ? def prepList[B >: A](prefix: PlayList[B]) : PlayList[B] = prefix.fol
我正在查看某人的代码,并且在创建结构时使用了 abstract type AbstractFoo end julia> struct Foo1 struct Foo2 foo_op(x::Abst
一些示例代码: public class Main { class SomeType { } class A { protected T createSome
是否可以只接受类的泛型类型的父类(super class)型? 我正在寻找的是这样的: class MyClass { public void myMethod(TS someObject
在我的代码中,我有许多 ArrayList 被传递到排序方法中。每个 ArrayList 都有不同的泛型类型,但所有这些类型都是 Sorter 的实现。排序方法旨在接受 Sorter 类型的 Arra
如果已经有人问过这个问题,请链接并关闭这个问题。 我目前正在为另一个使用起来复杂得多(并且有潜在危险)的 API 的简化 API 设计原型(prototype)。 考虑到相关的有点复杂的对象创建,我决
我正在尝试构建一个具有某些依赖项的 android 应用程序,但是其中一个导致了此错误: Illegal class file: Class module-info is missing a supe
我是一名优秀的程序员,十分优秀!