Vue / Typescript泛型的接口



在以下场景中允许扩展基类型的正确语法是什么?

interface SomeBase {}
interface A extends SomeBase {}
interface B extends SomeBase {}
interface Foo {
bar: // Can be an array of A or B
}

是只有bar: SomeBase[]还是有bar: <? extends SomeBase>之类的

我不想做bar: A[] | B[],因为可能有大量的接口,我想避免显式地将它们写出来。

我没有看到任何现成的文档将其定义为接口内的变量。

bar: SomeBase[]允许使用混合类型的数组。如果你想要一个,它应该是通用的。

interface SomeBase {}
interface A extends SomeBase {}
interface B extends SomeBase {}
type Foo <T extends SomeBase> = {
bar: T[];
}

最新更新