【问题标题】:How to use part of an interface as a new interface in Typescript?如何在 Typescript 中使用接口的一部分作为新接口?
【发布时间】:2020-01-16 11:48:07
【问题描述】:

是否可以将接口的一部分用作 Typescript 中的新接口? 例如我有下面的界面:

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

现在我只想使用一些Mobile界面数据,例如我只需要:

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

如何在手机界面使用手机界面数据?

【问题讨论】:

    标签: typescript interface


    【解决方案1】:

    您可以为此使用Pick util

    通过从 T 中选择一组属性 K 来构造一个类型

    interface Mobile {
        canCall: boolean;
        haveScreen: boolean;
        isItIOS: boolean;
        brand: string;
    }
    
    type Phone = Pick<Mobile, 'canCall' | 'brand'>;
    

    Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2020-09-20
      • 2020-10-26
      • 2022-08-03
      相关资源
      最近更新 更多