Kindzoku
Kindzoku

Reputation: 1420

Typescript: Extend interface with generic interface

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.

Upvotes: 0

Views: 222

Answers (1)

SET001
SET001

Reputation: 11728

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 { }

Upvotes: 2

Related Questions