简体   繁体   English

打字稿:至少实现特定接口的类

[英]Typescript: Class that implements at least a specific interface

I have the following question. 我有以下问题。 Let's say i have an Interface like this 假设我有一个这样的接口

interface Animal<T>{
   children: Array<T>
}

now i want to write a method which will later take in eg a bird (a class that implements Animal). 现在,我想编写一个方法,该方法以后将包含一个鸟(一个实现Animal的类)。 At the time writing the method the definition for bird does not exist yet. 在编写该方法时,鸟的定义尚不存在。 It could also be it would be given an instance of Shark. 也可能是它被赋予了Shark的实例。 But the minimal requirement will be that any object it receives will be an instance of a class that implements Animal. 但最低要求是,它接收的任何对象都将是实现Animal的类的实例。

function doSomething(AnAnimal){
   //Do something with animal
}

// Implementation with a class that implements Animal

class Bird<T> implements Animal{
   children: Array<T>;
   color: string;
   chirp(){console.log('peep')}

   constructor(Color:string){
      this.color = Color;
   }
}

let tweety = new Bird('yellow');

doSomething(Tweety)

I hope I could make clear what I'm trying to do and you have an idea how i could solve this in TypeScript. 我希望我能弄清楚我要做什么,您对我可以在TypeScript中解决这个问题有个想法。

You need to flow your type arguments throughout your example... 您需要在整个示例中传递类型参数。

interface Animal<T>{
   children: Array<T>
}

function doSomething<T>(arg: Animal<T>){
   //Do something with animal
}

class Bird<T> implements Animal<T>{
   children: Array<T>;
   color: string;
   chirp(){console.log('peep')}

   constructor(Color:string){
      this.color = Color;
   }
}

let tweety = new Bird('yellow');

doSomething(tweety);

For example, Bird<T> must implement Animal<T> . 例如, Bird<T>必须实现Animal<T> The doSomething function must be generic in order to deal with an argument of type Animal<T> . 为了处理Animal<T>类型的参数, doSomething函数必须是通用的。

Non-Generic Version 非通用版本

You might find that you are tying yourself up unnecesarily in type arguments here - so you might want to try the non-generic version. 您可能会发现您在这里不必要地在类型参数中使用了自己-因此您可能想尝试使用非通用版本。

interface Animal {
    children: Array<Animal>;
}

function doSomething(arg: Animal) {
    //Do something with animal
}

class Bird implements Animal {
    children: Array<Bird>;
    biggestEnemy: Animal;
    color: string;
    chirp() { console.log('peep') }

    constructor(Color: string) {
        this.color = Color;
    }
}

let tweety = new Bird('yellow');

doSomething(tweety);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript - 检查 Class 是否实现了接口 - TypeScript - Check if Class implements an Interface Typescript class 实现接口简化 - Typescript class implements interface simplify 打字稿:类实现接口问题? - Typescript: class implements interface issue? TypeScript:任何实现特定接口的类 - TypeScript: Any class that implements a particular interface TypeScript:实现某个接口或方法的 class 的类型 - TypeScript: type of class that implements a certain interface or method 我可以在一个类中定义一个特定的类类型,该类在打字稿中实现了一个具有泛型类型的接口吗? - Can I define a specific class type in a class that implements an interface with a generic type in typescript? 接口字段应具有实现该接口的类的类型-typescript - Interface field should have type of the class that implements that interface - typescript Typescript generics 对于类型 A 是 Class,B 是实现接口的 class<a></a> - Typescript generics for type A is a Class and B is a class that implements Interface<A> Typescript - 使函数返回类型成为接口实现的类 - Typescript - Make the function return type the class that an interface implements TypeScript 0.9.5:如何使用静态成员和实现它的类来定义接口? - TypeScript 0.9.5: how to define an interface with static members and a class that implements it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM