0

Let's say I have interface:

interface IAddress {
    addressProperty: any;
}

Is there a way to write interface similar to this:

interface ILoadable<T> {
    loading: boolean;
}

So I can use it like:

interface IStateSlice {
    address: ILoadable<IAddress>;
}

Where address will be ILoadable extended with IAddress.

1 Answer 1

2

I think you can use intersection types:

interface IAddress {
    addressProperty: any;
}

interface ILoadable {
    loading: boolean;
}

interface IStateSlice {
    address: ILoadable & IAddress
}

and then use it:

class StateSlice implements IStateSlice{
    address = {
        loading: false,
        addressProperty: 'lol'
    }
}

You can also define a separate type for that:

type LoadableAddress = ILoadable & IAddress

interface IStateSlice {
    address: LoadableAddress
}

or same, using interface extending

interface LoadableAddress extends ILoadable, IAddress { }
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! Thx! I never thought that you can use & this way :D
You can also do multiple implements separating them by comma. In addition you can extend multiple interfaces the same way, so you can create a class that extends both interlaces
@Kindzoku, then you can mark this answer as selected )

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.