假设我有
interface Animal {}
interface Group<A extends Animal> {}
Run Code Online (Sandbox Code Playgroud)
我想让一个接口在 Group 上通用
interface AnimalGroupProps<G extends Group<A>, A extends Animal> {
withGroup: () => G
// We want to be able to reference A in the interface, for use like this:
withLeader: () => A
}
Run Code Online (Sandbox Code Playgroud)
我希望动物组道具对于组类型来说是通用的。但 A 扩展了 Animal 似乎是多余的。我希望能够说:
interface AnimalGroupProps<G extends Group<A>>
Run Code Online (Sandbox Code Playgroud)
并让 TypeScript 解决它。但 TypeScript 希望声明 A,所以我必须使用前面的代码片段的模式。
class Wolf implements Animal {}
class WolfPack implements Group<Wolf> {}
function AnimalPlanetWolves ({withGroup, withLeader}: AnimalGroupProps<WolfPack, Wolf>) {}
// This is …Run Code Online (Sandbox Code Playgroud)