18

Say I have 2 interfaces:

interface Interface1{}
interface Interface2{}

is there a way to declare a property as implementing both interfaces? Something like:

class MyClass{
  public p: Interface1, Interface2
}

2 Answers 2

22

is there a way to declare a property as implementing both interfaces? Something like:

Yup. An intersection type:

interface Interface1{}
interface Interface2{}
class MyClass{
  public p: Interface1 & Interface2
}

More

Sign up to request clarification or add additional context in comments.

2 Comments

the link is now dead
@AntoineWeber The link is now again ALIVE!!! But it's also available at archive.org here. Maybe it was having a bad day.
4
interface Interface1 { }
interface Interface2 { }
interface ICombination extends Interface1, Interface2 { };
class MyClass {
    public p: ICombination
}

2 Comments

This works fine, but a disadvantage is that as the number of interfaces increases, the number of combinations grows very quickly. Depending on the use case it might not be a problem, but it's something to consider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.