【问题标题】:How to omit one property from interface, but not with type in TypeScript?如何从接口中省略一个属性,而不是 TypeScript 中的类型?
【发布时间】:2020-09-21 22:35:51
【问题描述】:

我需要创建一个从 2 扩展的接口,但我收到错误: 接口“IModalProps”不能同时扩展类型“ModalProps”和“ModalRNProps”。 “ModalProps”和“ModalRNProps”类型的命名属性“onShow”不相同。

export interface IModalProps extends ModalProps, ModalRNProps {
  showCloseButton?: boolean;
  showDoneBar?: boolean;
}

我只能省略这样的类型:

type OmitA = Omit<ModalProps, "onShow">; 

但是我不能在 make extends 之后使用类型,因为只有接口才有可能。你能告诉我如何从接口中省略一个属性,然后从几个接口创建一个可扩展接口?

【问题讨论】:

    标签: typescript react-native types


    【解决方案1】:

    尝试接口而不是类型

    export interface IModalProps {
      showCloseButton?: boolean;
      showDoneBar?: boolean;
    }
    
    
    export interface Test extends Omit<IModalProps, 'showDoneBar'> {
    
    }
    
    const test: Test = {
        showCloseButton: true,
        showDoneBar: false, // fails
     };
    

    Playground

    【讨论】:

    • 酷!非常感谢,我没有接口)))
    • 不客气,如果对您有用,请随时将其标记为正确答案:)
    猜你喜欢
    • 2019-09-06
    • 2021-05-16
    • 2021-07-06
    • 1970-01-01
    • 2021-09-16
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多