从扩展接口扩展联合类型

时间:2020-04-07 07:42:23

标签: typescript typescript-generics

如果我具有以下接口定义:

interface ModalProps {
    onClose: (event: {}, reason: 'backdropClick' | 'escapeKeyDown'): void;
}

现在,我要添加其他原因:

interface CustomModalProps {
    onClose: (event: {}, reason: 'backdropClick' | 'escapeKeyDown' | 'cancelClick'): void;
}

有什么方法可以从CustomModalProps内引用ModalProps onClose原因,这样我就不必复制值了?

interface CustomModalProps extends ModalProps {
  onClose: (event: {}, reason: ??? &| 'cancelClick'): void
}

1 个答案:

答案 0 :(得分:2)

我们的类型。

type ModalReason = 'backdropClick' | 'escapeKeyDown';

type CustomModaReason = ModalReason | 'cancelClick';

interface ModalProps {
    onClose: (event: {}, reason: ModalReason): void;
}

interface CustomModalProps {
    onClose: (event: {}, reason: CustomModaReason): void;
}