0

I have a base interface like:

interface base {
    property1: string
    property2: string;
    property3: [{
        something1: string;
        something2: string;
    }];
}

I would like to have an interface like:

property1: string
property2: string;
property3: [{
    something1: string;
    something2: string;
    something3: number;
}];
property4: number;

I was able to achieve the propert4 using extend like:

interface extended {
    property4: number
}

I also read up on how to extend a property and was doing it like this:

type extendedProperty = base['property3'];

interface extendedPropertyInterface extends extendedProperty {
    something3: number;
}
interface extended {
    property3: extendedPropertyInterface;
    property4: number;
}

This does not work. I see that extendedPropert is of the type

[{
    something1: string;
    something2: string;
}];

(Notice the Array brackets). How do I get what I want to achieve?

EDIT: I cannot change the base interface.

1
  • Generics might help here Commented Sep 17, 2017 at 10:59

1 Answer 1

1

First of all, this form: [type] declares a tuple and not an array, it should be: type[].

As for what you asked about, you can do this:

interface ArrayItem {
    something1: string;
    something2: string;
}

interface Base<T extends ArrayItem = ArrayItem> {
    property1: string
    property2: string;
    property3: T[];
}

interface ExtendedItem extends ArrayItem {
    something3: number;
}

interface Extended extends Base<ExtendedItem> {
    property4: number;
}

(code in playground)


Edit

As you cannot change base, this seems to work:

interface extended extends base {
    property3: [{
        something1: string;
        something2: string;
        something3: number;
    }];
    property4: number;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ah! I should have said this but I can't change the Base interface. Else, I would have done the same. I will make an edit to the question to that effect.
Check my revised answer
You can also reuse base type: property3: [base['property3'][0] & {something3: number}];
@AlekseyL.Awesome. That's exactly what I was looking for. Can you please explain what the & is doing here?

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.