似乎不可能有一个受联合类型约束的泛型类。基本上是这样:
class Shoe {
constructor(public size: number){}
}
class Dress {
constructor(public style: string){}
}
class Box <T extends Shoe | Dress > {
}
然后这样:
// because move can move multiple boxes of Box of shoes or Box of dress
class Move<B extends Box<Shoe>[] | Box<Dress>[]> {
private stuff: B;
constructor(public toMove: Box<Shoe>[] | Box<Dress>[]) {
this.stuff = toMove // this does not compile
}
}
或者这怎么实现?我有Playground link来说明情况
答案 0 :(得分:0)
我猜,这也是对您真实意图的猜想,这足够了:
class Shoe {
constructor(public size: number){}
}
class Dress {
constructor(public style: string){}
}
class Box <T extends Shoe | Dress > {
}
// because move can move multiple boxes of Box of shoes or Box of dress
class Move<B extends Box<Shoe>[] | Box<Dress>[]> {
private stuff: B;
constructor(public toMove: B) {
this.stuff = toMove;
}
}