Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I inherit one interface into another in typescript? How can I access the property defined in the inherited interface?

Two interface are there first one is ICat and second one is IMammal. IMammal extends ICat. Will the Cat property in IMammal have the capability to access all the property of ICat interface?

export interface ICat {
    Cateye: string[];
    Color: string;    
    Name: string;
}

export interface IMammal extends ICat {
    Description: string;
    HasImage: boolean;   
    CatGroup: string[];
    **Cat: ICat[]**;
}

Basically, how can I achieve multiple interface inheritances in Typescript?

like image 327
vishal0882 Avatar asked Oct 04 '17 12:10

vishal0882


People also ask

How do you inherit from another interface in TypeScript?

An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.

Can we inherit one interface to another interface?

C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.

How do you override properties in interface TypeScript?

Use the Omit utility type to override the type of an interface property, e.g. interface SpecificLocation extends Omit<Location, 'address'> {address: newType} . The Omit utility type constructs a new type by removing the specified keys from the existing type.

How do I combine two TypeScript interfaces?

To merge two interfaces with TypeScript, we can use extends to extend multiple interfaces. to create the IFooBar that extends IFoo and IBar . This means IFooBar has all the members from both interfaces inside.


2 Answers

I think maybe ICat should extend IMammal because Cat is a Mammal, and a Mammal do not need any references to ICat, think if you want to add IDog one day:

export interface IMammal {
    Description: string;
    HasImage: boolean;   
}

export interface ICat extends IMammal {
    Cateye: string[];
    CatGroup: string[];
    Color: string;    
    Name: string;
}

class Cat implements ICat {
    Cateye: string[];
    Color: string;    
    Name: string;
    Description: string;
    HasImage: boolean;   
    CatGroup: string[];
 }

const pusur:ICat = new Cat();
pusur.Name = "Pusur";
pusur.Description = "Likes lasagna";
pusur.CatGroup = ["Cartoon cat"];
like image 179
Gaotter Avatar answered Oct 03 '22 19:10

Gaotter


You use implements for interfaces, use extends for class inheritance. implements allows you to pass a list of interfaces which are implemented by the class.

Note that quite often it won't matter, as a class that implements all of the properties and methods of an interface is automatically compatible with the interface whether or not it explicitly implements the interface, but listing the interfaces explicitly at least means the compiler will tell you if you failed to implement them correctly.

interface A {
    a: string;
}

interface B {
    b: string;
}

class Foo implements A,B {
  a: string;
  b: string;
}

function foo(b: B) {}
function bar(a: A) {}

const f = new Foo();
foo(f);
bar(f);
like image 33
Duncan Avatar answered Oct 03 '22 20:10

Duncan