简体   繁体   English

扩展打字稿界面

[英]Extending typescript interfaces

I have component Checkbox.tsx , which ts interface looks like: 我有组件Checkbox.tsx ,其ts界面如下所示:

export interface ICheckbox {
    /** DOM element id. */
    id: string;
    /** Handler to the onClick event */
    onCheck?: (e: React.ChangeEvent<HTMLInputElement>) => void;
}

Now, I would like to create another component CheckboxWithLabel.tsx , which will have a very similar interface, the only additional thing gonna be a label. 现在,我想创建另一个组件CheckboxWithLabel.tsx ,它将具有非常相似的界面,唯一的附加功能就是标签。 How can I merge two interfaces or extends existing interface? 如何合并两个接口或扩展现有接口? Something like: 就像是:

import {ICheckbox} from './Checkbox.tsx';

export interface ICheckboxLabel {
    ...ICheckbox,
    label: string;
}

What i have tried is: 我试过的是:

export interface ICheckboxLabel extends ICheckbox{
     children: JSX.Element | JSX.Element[] | string;
}

The problem is, that I don't know if it's the correct approach. 问题是,我不知道这是否是正确的方法。

You can export an interface by using extends keyword. 您可以使用extends关键字导出接口。

interface a extends b {
   ...
}

In your example it will be like. 在您的示例中将是这样。

export interface ICheckboxLabel extends ICheckbox {
   label: string
}

Also visit the official documentation for more information https://www.typescriptlang.org/docs/handbook/interfaces.html 另请访问官方文档以获取更多信息https://www.typescriptlang.org/docs/handbook/interfaces.html

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

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