Taghi Khavari
Taghi Khavari

Reputation: 6582

How to use part of an interface as a new interface in Typescript?

Is it possible to use part of an interface as new interface in Typescript? for example I have interface below:

interface Mobile{
 canCall: boolean;
 haveScreen: boolean;
 isItIOS: boolean;
 brand: string;
}

Now I want to use just some of Mobile interface data, for example I only need :

interface phone{
 canCall: boolean;
 brand: string;
}

how Can I use Mobile interface data in phone interface?

Upvotes: 2

Views: 167

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 37918

You can use Pick util for this

Constructs a type by picking the set of properties K from T

interface Mobile {
    canCall: boolean;
    haveScreen: boolean;
    isItIOS: boolean;
    brand: string;
}

type Phone = Pick<Mobile, 'canCall' | 'brand'>;

Playground

Upvotes: 4

Related Questions