简体   繁体   English

如何从接口中省略一个属性,但不是 TypeScript 中的类型?

[英]How to omit one property from interface, but not with type in TypeScript?

I need to make one interface that extends from 2 another, but I get the error: Interface 'IModalProps' cannot simultaneously extend types 'ModalProps' and 'ModalRNProps'.我需要创建一个从 2 扩展的接口,但我收到错误:接口 'IModalProps' 不能同时扩展类型 'ModalProps' 和 'ModalRNProps'。 Named property 'onShow' of types 'ModalProps' and 'ModalRNProps' are not identical. “ModalProps”和“ModalRNProps”类型的命名属性“onShow”不相同。 :

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

I can do omit only with type like this:我只能省略这样的类型:

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

But I can not after make extends with type, because it is possible only with interfaces.但是我不能在使用类型进行扩展之后,因为只有接口才有可能。 Can you tell me please how can I omit one property from the interface and after create one extendable interface from a few interfaces?你能告诉我,我怎样才能从接口中省略一个属性,然后从几个接口创建一个可扩展接口?

try interfaces instead of types尝试接口而不是类型

export interface IModalProps {
  showCloseButton?: boolean;
  showDoneBar?: boolean;
}


export interface Test extends Omit<IModalProps, 'showDoneBar'> {

}

const test: Test = {
    showCloseButton: true,
    showDoneBar: false, // fails
 };

Playground 操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM