Rajab Shakirov
Rajab Shakirov

Reputation: 8045

an interface may only extend a class or another interface / mapped types

I use typescript 2.1 along with JSX and I try to use Partial mapped type for extend interface of react component with optional properties from interface with required properties.

this is interface with required properties:

interface ActionsCreators {
  A: string;
  B: string;
};

this is interface which I want to extend:

interface Props extends React.Props<any> {
  C: string;
};

this is mapped type:

type ActionsCreatorsPartial = Partial<ActionsCreators>;

which give this result:

type ActionsCreatorsPartial = {
  A?: string;
  B?: string;
}

this is what I try to do:

interface Props extends React.Props<any>, ActionsCreatorsPartial {
   C: string;
};

but I see compilation error: "an interface may only extend a class or another interface" How can I connect interface Props with ActionsCreatorsPartial type?

Upvotes: 2

Views: 8555

Answers (1)

Alex
Alex

Reputation: 14523

The error message is correct, interfaces can only extend other interfaces or the interface of a class, and classes can likewise only implement interfaces or take the interface from another class.

There is an approved suggestion to let classes implement type aliases, I guess it would be in scope to also let interfaces extend type aliases if that is implemented.

Unfortunately, as of today, I don't think that there is any way to incorporate a mapped type into a interface.

Upvotes: 2

Related Questions